2015-07-03 88 views
0

我試着寫RSpec的測試我禮包自定義變量,我需要創造產品的變種。即使我看起來和rspec測試完全一樣,但它是spree核心的一部分,我似乎無法做到這一點。爲施普雷寫作測試,不能創造產品

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 

無論我做什麼,我的產品變體的數量總是爲零。

回答

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