0

我有一个Factory创建一个 mock 的order。它before_create在工厂中有一个用于创建 3 个模型,这些模型after_create在创建order. 以下工厂代码如下所示(我意识到这有点混乱,但这是我目前试图解决我的问题的方法):

    FactoryGirl.define do
      factory :order, :class => Spree::Order do
    # associations:
    association(:user, :factory => :user)
    association(:bill_address, :factory => :address)
    association(:ship_address, :factory => :address)
    completed_at nil
    bill_address_id nil
    ship_address_id nil
    email 'foo@example.com'

    before(:create) do |order|
      country = Spree::Country.find_by_id(214).blank? ? create(:country, :id => 214) : Spree::Country.find_by_id(214)
      state = create(:state, :country => country, :country_id => country.id)
      country.states = [state]
      FactoryGirl.create(:address, :id => 1072978592,:state => state, :country => country) if Spree::Address.find_by_id(1072978592).blank?
    end
  end
    end

但是,当我运行测试时,我不断收到如下错误:

  1) Spree::Calculator::DefaultTax#compute when given an order when no line items match the tax category should be 0
 Failure/Error: let!(:order) { create(:order) }
 ActiveRecord::RecordInvalid:
   Validation failed: Country can't be blank
 # ./spec/models/calculator/default_tax_spec.rb:8:in `block (2 levels) in <top (required)>'

它给出了国家不能为空的验证错误。我不确定我做错了什么。我在地址和州中正确分配了国家。

rspec  2.13.0,
factory_girl_rails 4.2.1 (factory _girl 4.2.0),
ruby 1.9.3,
rails 3.2.13
spree 1.2.4
4

1 回答 1

0

由于 FactoryGirl 的语法更新,您的回调可能无法运行。 http://robots.thoughtbot.com/post/23039827914/get-your-callbacks-on-with-factory-girl-3-3

尝试:

after(:create)

代替:

after_create

您也可以尝试将您的 FactoryGirl 版本降级到 3.3 之前的版本。

于 2013-06-22T12:23:56.800 回答