Rspec:在其他工厂使用工厂

时间:2017-02-17 17:41:50

标签: ruby-on-rails ruby rspec factory-bot

我正在编写一个为帖子添加评论的创建方法的费用。

评论属于用户和帖子。并且帖子属于用户。

当我运行测试时,我收到验证错误,说明已经使用了用户名和电子邮件。我尝试在我的工厂和测试中使用build和build_stubbed,但它们都没有工作。我认为这与我使用create这个事实有关,但我并不完全确定。

非常感谢任何建议

这是我的工厂:

users.rb

FactoryGirl.define do
factory :user do
    username "test_user"
    email "test_user@email.com"
    password "password"
end

factory :user_2, class: User do
    username "test_user_2"
    email "test_user_2@email.com"
    password "password"
end

factory :invalid_user, class: User do
    username ""
    email ""
    password ""
end
end
outlets.rb

FactoryGirl.define do
  factory :outlet do
    category "vent"
    title "MyString"
    body "MyText"
    urgency 1
    user factory: :user
  end

  factory :outlet_2, class: Outlet do
    category "rant"
    title "MyString_2"
    body "MyText_2"
    urgency 2
    user factory: :user_2
  end

  factory :invalid_outlet, class: Outlet do
    category "qualm"
    title ""
    body ""
    urgency 3
    user factory: :user
  end
end
comments.rb

FactoryGirl.define do
  factory :comment do
    body "This is a comment"
    user factory: :user
    outlet factory: :outlet_2
  end

  factory :invalid_comment, class: Comment do
    body "This is a comment"
    user nil
    outlet nil
  end  
end

这是我的测试:

describe 'create' do
        context 'with valid attributes' do

            let(:outlet) { FactoryGirl.create(:outlet) }
            let(:valid_comment_params) { FactoryGirl.attributes_for(:comment) }

            it "creates a new comment" do
                expect { post :create, params: { id: outlet, :comment => valid_comment_params } }.to change(Comment, :count).by(1)
            end
        end
    end

以下是我的模特:

class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :outlet

  validates :body, :user, :outlet, presence: true
  validates :body, length: { in: 1..1000 }
end
class Outlet < ApplicationRecord
  belongs_to :user
  has_many :comments

  validates :category, :title, :body, :urgency, :user, presence: true
  validates :title, length: { in: 1..60 }
  validates :body, length: { in: 1..1000 }
  validates :urgency, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 10 }
  validates :category, inclusion: { in: ['vent', 'rant', 'qualm'] }
end
class User < ApplicationRecord
    has_many :outlets
    has_many :comments

    validates :username, :email, :encrypted_password, presence: true
    validates :username, :email, uniqueness: true
    validates :password, length: { in: 5..30 }
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable 
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable
end

1 个答案:

答案 0 :(得分:0)

所以这里的问题是你一直在尝试用你刚创建另一个用户的同一个电子邮件和用户名创建一个用户。为了避免在您的工厂中出现这种情况,您应该努力使价值变为动态。由于目前的主要问题是唯一性验证,让我们从那些开始。

factory :user do
  sequence(:username) { |n| "test_user#{n}" }
  sequence(:email)    { |n| "test_user#{n}@email.com" }
  password "password"
end

这样,您可以使用同一工厂创建2个不同的用户

user = FactoryGirl.create :user
user_2 = FactoryGirl.create :user