2017-02-17 75 views
0

我正在寫一個創建方法的成本,它會爲帖子添加評論。Rspec:在其他工廠中使用工廠

該評論屬於用戶和帖子。而帖子屬於用戶。

當我運行我的測試時,出現驗證錯誤,說明用戶名和電子郵件已被佔用。我試過在我的工廠和測試中都使用build和build_stubbed,但他們都沒有工作。我認爲這與我使用create相關,但我不完全確定。

任何建議,將不勝感激

這裏是我的工廠:

users.rb 

FactoryGirl.define do 
factory :user do 
    username "test_user" 
    email "[email protected]" 
    password "password" 
end 

factory :user_2, class: User do 
    username "test_user_2" 
    email "[email protected]" 
    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 
+0

您是否介意發佈您的型號代碼以及(特別是驗證)?這將有助於指導答案。 – Sean

+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 
+0

進行了編輯當我嘗試這樣做時,我得到「無法找到id =」或者它應該將計數改爲1,但在您想要做的測試中它被更改爲0 –

+0

'expect {post:create,params:{:comment => valid_comment_params}} .to改變(Comment,:count).by(1)'你不需要'id'的值,因爲你沒有創建對象但(它沒有一個ID,直到它被持續) – Sean