工厂女孩与产品模型:Rspec告诉我"Trait not registered"



大家好!

对于有时间的人来说,还是一个有基本问题的新手;-)

我创建了一个用户模型和一个产品模型,像这样(感谢@grotori):

class User
  has_many :owned_products, class_name: "Product", foreign_key: "owner_id"
  has_many :borrowed_products, class_name: "Product", foreign_key: "borrower_id"
end
class Product
    belongs_to :owner, class_name: "User", foreign_key: "owner_id"
    belongs_to :borrower, class_name: "User", foreign_key: "borrower_id"
end

然后我使用FactoryGirl创建一个工厂。像这样:

FactoryGirl.define do
  factory :user do
    sequence(:name) { |n| "Person #{n}" }
    sequence(:email) { |n| "Person_#{n}@example.com" }
    password "kaboum"
    password_confirmation "kaboum"
  end
  factory :product do
    name "Product"
    owner
    borrower
  end
end

为了测试dependent: :destroy条件,如下:

describe User do
  before do
    @user = User.new(name: "Example User",
                        email: "user@example.com",
                        password: "kaboum",
                        password_confirmation: "kaboum")
  end
  describe "product associations" do
    before { @user.save }
    let!(:product) { FactoryGirl.create(:product, owner: @user) }
    it "should destroy associated product" do
      product_to_be_destroyed = @user.owned_products.first
      @user.destroy
      expect(product_to_be_destroyed).not_to be_empty
      expect(Product.where(id: product_to_be_destroyed.id)).to be_empty
    end
  end
end

Rspec给我答案ArgumentError: Trait not registered: owner

我在stackoverflow上搜索了已经回答的问题,但我无法解决我的问题。一种解决方案是为工厂中的"所有者"或"借方"字段分配默认值。但是我不希望那样,这在FactoryGirl帮助下是可能的。

任何帮助我们都将感激不尽:-)

我相信这可以在不创建额外的所有者和借款人工厂的情况下解决:

  factory :product do
    name "Product"
    association :owner, factory: :user
    association :borrower, factory: user
  end

正如@kiddorails所说,我只需要为我的所有者和借款人添加特定的工厂。现在它来了(在user_spec中略有不同)。rb)部分:

规范/factories.rb

  factory :owner, :class => "User" do
    sequence(:name) { |n| "Owner #{n}" }
    sequence(:email) { |n| "owner_#{n}@example.com" }
    password "kaboum"
    password_confirmation "kaboum"
  end
   factory :borrower, :class => "User" do
    sequence(:name) { |n| "Borrower #{n}" }
    sequence(:email) { |n| "borrower_#{n}@example.com" }
    password "kaboum"
    password_confirmation "kaboum"
  end
  factory :product do
    sequence(:name) { |n| "Product #{n}" }
    owner
    borrower
  end

规范/模型/user_spec.rb

  describe "product associations" do
    before { @user.save }
    let!(:product) { @user.owned_products.create(name:"tabouret") }
    it "should destroy the associated products" do
      product_id = @user.owned_products.first.id
      @user.destroy
      expect(product_id).not_to be_nil
      expect(Product.where(id: product_id)).to be_empty
    end
  end

恐怕联想关系不对。没有必要在正确的模型之前浪费时间调试FactoryGirl。

一般来说,一个产品可以被许多用户借用或购买。用户可以借阅或购买许多产品。has_many和belongs_to的关系是怎样的?

也许这样的造型会更好

class User < ActiveRecord::Base
  has_many :orders
  has_many :borrrow_records
end
class Order < ActiveRecord::Base
  belongs_to :user
  belongs_to :product
end
class BorrowRecord < ActiveRecord::Base
  belongs_to :user
  belongs_to :product
end      
class Product < ActiveRecord::Base
  has_many: orders
  has_many: borrow_records
end

最新更新