factory_girl 不能很好地使用 rspec2 - ActiveRecord::AssociationTypeMismatch
我使用 Rails 3.1.0rc8、Factory Girl 2.1.2、Factory Girl Rails 1.2.0 和 RSpec 2.7.0。
我相信我遇到的错误与此帖子。
我有一个如下所示的规范:
spec/integration/my_integration_spec.rb:
require 'spec_helper'
describe 'A Workflow' do
before(:all) do
@reseller = Factory(:reseller)
@product = Factory(:product, :reseller => @reseller)
end
describe 'A feature' do
it 'Does something' do
end
describe 'A sub-feature' do
before(:all) do
# Error!
@product.sold_at << Factory(:outlet, :reseller => @reseller)
end
it 'Does something' do
end
end
end
运行此规范会导致子功能出现异常:
Failure/Error: @product.sold_at << Factory(:outlet, :reseller => @reseller)
ActiveRecord::AssociationTypeMismatch:
Reseller(#90828680) expected, got Reseller(#59351220)
有趣的是,此错误不会发生当我将嵌套前挂钩的内容移动到主前挂钩中时。
require 'spec_helper'
describe 'A Workflow' do
before(:all) do
@reseller = Factory(:reseller)
@product = Factory(:product, :reseller => @reseller)
# No error!
@product.sold_at << Factory(:outlet, :reseller => @reseller)
end
describe 'A feature' do
it 'Does something' do
end
describe 'A sub-feature' do
it 'Does something' do
end
end
end
非常感谢您对理解这个问题的任何帮助。
Am using Rails 3.1.0rc8, Factory Girl 2.1.2, Factory Girl Rails 1.2.0, and RSpec 2.7.0.
I believe the error I am having is related to the problems discussed on this thread.
I have a spec that looks like this:
spec/integration/my_integration_spec.rb:
require 'spec_helper'
describe 'A Workflow' do
before(:all) do
@reseller = Factory(:reseller)
@product = Factory(:product, :reseller => @reseller)
end
describe 'A feature' do
it 'Does something' do
end
describe 'A sub-feature' do
before(:all) do
# Error!
@product.sold_at << Factory(:outlet, :reseller => @reseller)
end
it 'Does something' do
end
end
end
Running this spec causes an exception in the sub-feature:
Failure/Error: @product.sold_at << Factory(:outlet, :reseller => @reseller)
ActiveRecord::AssociationTypeMismatch:
Reseller(#90828680) expected, got Reseller(#59351220)
Interestingly, this error does not occur when I move the content of the nested before-hook into the primary before-hook.
require 'spec_helper'
describe 'A Workflow' do
before(:all) do
@reseller = Factory(:reseller)
@product = Factory(:product, :reseller => @reseller)
# No error!
@product.sold_at << Factory(:outlet, :reseller => @reseller)
end
describe 'A feature' do
it 'Does something' do
end
describe 'A sub-feature' do
it 'Does something' do
end
end
end
Would really appreciate any help in understanding this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个规范文件中只能有一个
before(:all)
。在规范顶部使用before(:all),然后在描述块中使用
before(:each)。这就是您的第二个示例有效的原因,您已删除了第二个
before(:all)`。另外,要小心
before(:all)
。此处创建的任何数据都不会在规范结束时从数据库中删除,您需要在after(:all)
中删除它或使用数据库清理器 gem。请参阅链接进行推理。You can only have one
before(:all)
in each spec file. Usebefore(:all) at the top of a spec and then
before(:each)in a describe block. This is the reason that your second example works, you have removed the second
before(:all)`.Also, be careful with
before(:all)
. Any data created here will not be deleted from the database at the end of a spec, you will need to delete it in anafter(:all)
or use the database cleaner gem. See link for reasoning.