我正在为我正在开发的Rails应用构建一个基本购物车。
没有什么特别的,
- 购物车有许多line_item
- 每个line_item与一个product相关联,并带有一个数量
我正在尝试使用RSpec测试这个关联,但是我做错了什么,因为我收到一个错误,上面写着:
在我的factories.rb文件中,我定义了line_item工厂,如下所示:
class Cart < ActiveRecord::Base
attr_accessible :line_items
has_many :line_items, :dependent => :destroy
end
class LineItem < ActiveRecord::Base
attr_accessible :quantity, :product
belongs_to :cart
has_one :product
end
我正在尝试使用RSpec测试这个关联,但是我做错了什么,因为我收到一个错误,上面写着:
DEPRECATION WARNING: You're trying to create an attribute 'line_item_id'. Writing arbitrary attributes on a model is deprecated
,我不确定为什么。在我的factories.rb文件中,我定义了line_item工厂,如下所示:
factory :line_item do
quantity { Random.rand(1..5) }
product
end
factory :cart do
factory :cart_with_two_line_items do
ignore do
line_item_count 2
end
after(:create) do |cart, evaluator|
FactoryGirl.create_list(:line_item, evaluator.line_item_count, cart_id: cart) # < 104
end
end
end
请问我哪里做错了,可能是一些基础问题,但我还是Rspec的新手。提前感谢。
编辑:line_item_spec.rb
require 'spec_helper'
describe LineItem do
before do
@line_item = FactoryGirl.create(:line_item)
end