0

嗨,我正在学习在 ruby​​ 中使用 Rspec 编写测试用例,并且正在关注此链接。所以在测试以下案例时

 require 'spec_helper'

describe "Library object" do
before :all do
    lib_obj = [
        Book.new ("Javascript: The Good Parts", "Douglas Crockford", :development),
        Book.new ("Designing with Web Standards", "Jeffrey Zeldman", :design),
        Book.new ("Don't make me Think", "Steve krug", :usability),
        Book.new ("Javascript Patterns", "Stoyam Stefanov", :development),
        Book.new ("Responsive Web Design", "Ethan Marcotte", :design)
    ]       
    File.open "books.yml", "w" do |f|
        f.write YAML::dump lib_obj
    end
end

before :each do
    @lib = Library.new "books.yml"
end

describe "#new" do
    context "with no parameters" do
        it "has no books" do
            lib = Library.new
            lib.should have(0).books
        end
    end
    context "with a yaml file paramater" do
        it "has five books" do
            @lib.should have(5).books
        end
    end
end

it "returns all the books in a given category" do
    @lib.get_books_in_category(:development).length.should == 2
end

it "accepts new books" do
    @lib.add_book(Book.new("Designing for the Web", "Mark Boulton", :design))
    @lib.get_book("Designing for the Web").should be_an_instance_of Book
end

it "saves the library" do
    books = @lib.books.map { |book| book.title  }
    @lib.save
    lib2 = Library.new "books.yml"
    books2 = lib2.books.map { |book| book.title  }
    books.should eql books2
end

结尾

我收到以下错误:-

syntax error, unexpected ',', expecting keyword_end
 Book.new ("Don't make me Think", "Steve krug", :usability),

这代表数组 lib_obj 中的所有条目。我正在使用 ruby​​ 1.9.3 和 rails 3.2.6

请帮助

4

2 回答 2

3

您的方法调用和参数列表之间有一个额外的空间。

Book.new (...)

...与以下内容不同:

Book.new(...)
于 2012-06-26T09:09:32.633 回答
2

我看到您正在为新的 Book 类传递字符串值,尽管您没有指定要将其绑定到的属性。

尝试:

new_book = Book.new(:name => "string", :cover_image => "string") etc.

还要确保您的模型验证和 mass_assignment 安全设置正确,但您的测试很快就会将您指向那里。

于 2012-06-26T09:08:23.043 回答