2013-03-02 89 views
0

在我的Rails应用程序我有各种ItemsInvoice如何使用RSpec单元测试accep_nested_attributes_for?

class Invoice < ActiveRecord::Base 

    attr_accessible :date, :recipient, :items_attributes 

    accepts_nested_attributes_for :items, :reject_if => :all_blank, :allow_destroy => true 

end 

不过,我与该模型一个RSpec的单元测试挣扎。例如,我想测试一个发票如果没有项目就会报错。

这不起作用:

describe Invoice do 

    it "is invalid without an item" do 
    expect(build(:invoice, :items_attributes => {}).to have(1).errors_on(:items_attributes) 
    end 

end 

有人能帮忙吗?

回答

1

从accepts_nested_attributes_for documentation

You may also set a :reject_if proc to silently ignore any new record hashes if they fail to pass your criteria. 

文件没有指定他们的意思默默但也可能是未抛出异常....

尝试建立一些验证,在项目模型属性,肯定会抛出一些异常结束应该足够响亮rspec期望{..}。阻止工作。