Factory.build 和 Factory.attributes_for 有什么区别?

发布于 2024-12-29 04:50:43 字数 1637 浏览 0 评论 0原文

在 Rails 3.1.0 应用程序的 rspec 测试中,我们同时使用 Factory.build 和 Factory.attributes_for。我们发现,如果我们将Factory.build更改为Factory.attributes_for,数据验证的一种情况会失败。此外,Factory.attributes_for 没有正确测试它。我想知道这两者之间有什么区别,以及如何在 rspec 中使用它们。

在我们的模型测试中,我们使用 Factory.build。在更新或新的控制器测试中,我们使用 Factory.attributes_for。我们刚刚在控制器测试中发现了一个案例,Factory.attributes_for 没有正确测试它,导致已通过 Factory.build 模型验证的案例失败。

非常感谢。

更新: 这是 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

这是 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

上面的控制器案例因验证失败而失败。控制器案例中的失败是由于在创建控制器 rfqs 时添加以下行引起的。

  @rfq.report_language = nil unless params[:need_report]

然而,rfq 模型中的案例(参见上文 rfq 模型)已成功通过。模型测试中为.build,控制器测试中为.attributes_for。

更新:

正确的语句应该是:

@rfq.report_language = nil unless params[:rfq][:need_report] == 'true'

OR

@rfq.report_language = nil if params[:rfq][:need_report] == 'false'

params[:need_report] 不返回任何内容,并且不是从 params 检索数据的正确方法。

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.

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.

Thanks so much.

UPDATE:
Here is a rspec case in rfq model:

  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:

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.

  @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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2025-01-05 04:50:43

Factory.attributes_for 仅返回工厂属性的哈希值。 Factory.build 使用这些相同的资源,但返回具有相同属性集的类的实例。

Factory.build(:user)

功能上等同于

User.new(Factory.attributes_for(:user))

,但你会发现它们不可互换。也许如果您发布一些代码,我们可以更好地解释您的测试中发生的情况。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文