factory_girl 不能很好地使用 rspec2 - ActiveRecord::AssociationTypeMismatch

发布于 2024-12-18 20:37:53 字数 1516 浏览 1 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(1

永言不败 2024-12-25 20:37:53

每个规范文件中只能有一个 before(:all)。在规范顶部使用 before(:all),然后在描述块中使用before(:each)。这就是您的第二个示例有效的原因,您已删除了第二个before(:all)`。

另外,要小心 before(:all)。此处创建的任何数据都不会在规范结束时从数据库中删除,您需要在 after(:all) 中删除它或使用数据库清理器 gem。请参阅链接进行推理。

You can only have one before(:all) in each spec file. Use before(:all) at the top of a spec and thenbefore(:each)in a describe block. This is the reason that your second example works, you have removed the secondbefore(: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 an after(:all) or use the database cleaner gem. See link for reasoning.

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