3

嗨神奇的社区!

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

我创建了这样的用户模型和产品模型(感谢@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 创建一个 factory.rb ,如下所示:

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上搜索了已经回答的问题,但我无法解决我的问题。一种解决方案是为我的 factory.rb 中的“所有者”或“借款人”字段分配一个默认值,但我不希望这样,这可以根据FactoryGirl 帮助进行。

任何帮助将不胜感激:-)

4

3 回答 3

3

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

  factory :product do
    name "Product"
    association :owner, factory: :user
    association :borrower, factory: user
  end
于 2015-09-01T15:34:30.840 回答
2

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

规格/工厂.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
于 2013-05-15T13:30:03.037 回答
0

恐怕关联关系是错误的。无需在正确模型之前浪费时间调试 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
于 2013-05-15T07:48:14.380 回答