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