0
一直在努力完成正確的測試,但我無法弄清楚。Rspec無法讓控制器創建動作來工作
註釋屬於用戶和出口 每個插座有一個用戶 每個出了許多意見 每個用戶都有網點衆多
在我得到這個錯誤的時刻:
Failure/Error: let(:outlet) { FactoryGirl.build(:outlet) }
ActiveRecord::RecordInvalid:
Validation failed: Username has already been taken, Email has already been taken
我真的不知道還有什麼可以嘗試的。我嘗試過切換工廠和圍繞一堆測試,但只有不同的錯誤。任何幫助將不勝感激
工廠:
FactoryGirl.define do
factory :comment do
body "This is a comment"
user
outlet
end
factory :invalid_comment, class: Comment do
body "This is a comment"
user nil
outlet nil
end
end
FactoryGirl.define do
factory :outlet do
category "vent"
title "MyString"
body "MyText"
urgency 1
user
end
factory :invalid_outlet, class: Outlet do
category "qualm"
title ""
body ""
urgency 3
user factory: :user
end
end
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "test_user#{n}" }
sequence(:email) { |n| "test_user#{n}@email.com" }
password "password"
end
factory :invalid_user, class: User do
username ""
email ""
password ""
end
end
測試
describe 'create' do
context 'with valid attributes' do
let(:outlet) { FactoryGirl.create(:outlet) }
let(:comment_params) { FactoryGirl.attributes_for(:comment) }
let(:create) { post :create, params: { id: outlet , comment: comment_params } }
it "creates new comment" do
puts outlet
puts comment_params
expect { create }.to change { Comment.count }.by 1
end
end
end
class CommentsController < ApplicationController
def new
@comment = Comment.new
end
def create
@outlet = Outlet.find(params[:id])
@comment = @outlet.comments.build(comment_params)
if @comment.save
redirect_to(@outlet)
end
end
private
def comment_params
params.require(:comment).permit(:body, :outlet_id, :user_id)
end
end
你說的錯誤是由'FactoryGirl.build(:outlet)'造成的,但它並沒有出現在代碼中的任何地方。這全部是最新的嗎? – mroach