为Spree编写测试,无法为产品创建变体

时间:2015-07-03 19:46:32

标签: ruby-on-rails rspec spree

我试图为我的狂欢定制编写rspec测试,我需要创建带有变体的产品。我似乎无法做到这一点,即使我似乎做的是与作为狂欢核心一部分的rspec测试完全相同的事情。

def build_option_type_with_values(name, values)
  ot = create(:option_type, :name => name)
  values.each do |val|
    ot.option_values.create(:name => val.downcase, :presentation => val)
  end
  ot
end


let(:number_size_option_type) do
  size = build_option_type_with_values("number sizes", %w(1 2 3 4))
end

let(:product1) { create(:product, name: 'product1') }

it "should have variants" do
  hash = {number_size_option_type.id.to_s => number_size_option_type.option_value_ids}
  product1.option_values_hash = hash
  product1.save
  product1.reload
  expect(product1.variants.length).to eq(4)
end

无论我做什么,我产品的变种数量始终为零。

1 个答案:

答案 0 :(得分:0)

在产品创建期间需要添加product.option_values_hash以调用变体创建代码。这是更改的行,然后我从测试“应该有变种”中删除了哈希

let(:product1) { create(:product, name: 'product1', option_values_hash: {number_size_option_type.id.to_s => number_size_option_type.option_value_ids}) }

it "should have variants" do
  product1.save
  expect(product1.option_type_ids.length).to eq(1)
  expect(product1.variants.length).to eq(4)
end