1

我已经成功测试了品牌(has_many :products)和产品(belongs_to :brand)之间的关联。现在我很想知道是否可以在 Rspec 中测试用户(has_many :brands、has_many 产品、:through => :brands)与其品牌产品之间的关联。下面的代码是我想到的,但显然它不起作用。

describe "user association" do
before(:each) do
  @brand_attr = { :name => "Apple", 
                  :established => "1976", 
                  :description => "Born at the SV Homebrew"
                }
  @user = Factory(:user)
  @brand = @user.brands.new(@brand_attr)
  @product = @brand.products.new(@attr)
end

it "should have a user attribute through the brand" do
  @product.brand.user.should == @user
end

谢谢!

4

0 回答 0