在rspec中编写测试用例时出错

时间:2012-06-26 08:58:57

标签: ruby-on-rails ruby testing rspec

您好我正在学习使用ruby中的Rspec编写测试用例并遵循此link。所以在测试以下情况时

 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

请帮助

2 个答案:

答案 0 :(得分:3)

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

Book.new (...)

......与以下内容不同:

Book.new(...)

答案 1 :(得分:2)

虽然您没有指定要将其绑定到的属性,但我发现您正在为新的Book类传递字符串值。

尝试:

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

还要确保您的模型验证和mass_assignment安全性设置正确,但您的测试会很快为您指出。