In our rspec test for rails 3.1.0 app, we use both Factory.build and Factory.attributes_for. What we found is that if we change the Factory.build to Factory.attributes_for, one case for data validation failed. Furthermore the Factory.attributes_for did not test it right. I am wondering what's the difference between those two and, how to use them in rspec.
在我们的rails 3.1.0应用的rspec测试中,我们使用了两个工厂。构建和Factory.attributes_for。我们发现如果我们改变工厂。建立工厂。attributes_for,数据验证失败的一种情况。此外,工厂。attributes_for没有对其进行正确的测试。我想知道这两者之间有什么区别,如何在rspec中使用它们。
In our model test, we are using Factory.build. In controller test for update or new, we use Factory.attributes_for. we just found out a case in controller test that Factory.attributes_for did not test it right and failed the case which has passed in model validation with Factory.build.
在我们的模型测试中,我们使用Factory.build。在用于更新或new的控制器测试中,我们使用Factory.attributes_for。我们刚刚发现了一个用控制器测试工厂的例子。attributes_for没有对其进行正确测试,并且在与Factory.build进行的模型验证中通过的案例中失败。
Thanks so much.
非常感谢。
UPDATE: Here is a rspec case in rfq model:
更新:这是rfq模型中的rspec案例:
it "should not have nil in report_language if need_report is true" do
rfq = Factory.build(:rfq, :need_report => true, :report_language => nil)
rfq.should_not be_valid
end
Here is a rspec case in rfq controller:
这是rfq控制器的rspec案例:
it "should be successful for corp head" do
session[:corp_head] = true
session[:user_id] = 1
s = Factory(:standard)
rfq = Factory.attributes_for(:rfq, :need_report => true, :report_language => 'EN')
rfq[:standard_ids] = [s.id] # attach standard_id's to mimic the POST'ed form data
get 'create', :rfq => rfq
response.should redirect_to URI.escape("/view_handler?index=0&msg=RFQ saved!")
end
The controller case above failed because of the validation failure. The failure in controller case is caused by addition of the line below to the create of controller rfqs.
上面的控制器案例由于验证失败而失败。控制器情况下的失败是由于在创建控制器rfqs时添加了下面的行而导致的。
@rfq.report_language = nil unless params[:need_report]
However the case in rfq model (see above for rfq model) has passed successfully. It is .build in model test and .attributes_for in controller test.
然而,rfq模型(参见上面的rfq模型)中的例子已经成功通过。它是。build in model test和。attributes_for in controller test。
UPDATE:
更新:
The right statement should be:
正确的说法应该是:
@rfq.report_language = nil unless params[:rfq][:need_report] == 'true'
OR
或
@rfq.report_language = nil if params[:rfq][:need_report] == 'false'
params[:need_report]
returns nothing and is not the right way to retrieve data from the params.
params[:need_report]不返回任何内容,并且不是从params检索数据的正确方法。
1 个解决方案
#1
6
Factory.attributes_for
simply returns a hash of the attributes for the Factory. Factory.build
uses these same assets, but returns an instance of your class with those same attributes set.
工厂。attributes_for仅返回工厂属性的散列。工厂。build使用这些相同的资产,但是返回具有相同属性集的类的实例。
Factory.build(:user)
is functionally equivalent to
在功能上相当于
User.new(Factory.attributes_for(:user))
but you'll see they're not interchangeable. Maybe if you post some of your code we can better explain what's going on in your tests.
但是你会发现它们是不可互换的。也许如果你发布一些代码,我们可以更好地解释你的测试中发生了什么。
#1
6
Factory.attributes_for
simply returns a hash of the attributes for the Factory. Factory.build
uses these same assets, but returns an instance of your class with those same attributes set.
工厂。attributes_for仅返回工厂属性的散列。工厂。build使用这些相同的资产,但是返回具有相同属性集的类的实例。
Factory.build(:user)
is functionally equivalent to
在功能上相当于
User.new(Factory.attributes_for(:user))
but you'll see they're not interchangeable. Maybe if you post some of your code we can better explain what's going on in your tests.
但是你会发现它们是不可互换的。也许如果你发布一些代码,我们可以更好地解释你的测试中发生了什么。