从第10.23章开始,文件:spec / requests / authentication_pages_spec.rb
require 'spec_helper'
describe "Authentication" do
.
.
.
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
.
.
.
describe "in the Microposts controller" do
describe "submitting to the create action" do
before { post microposts_path }
specify { expect(response).to redirect_to(signin_path) }
end
describe "submitting to the destroy action" do
before { delete micropost_path(FactoryGirl.create(:micropost)) }
specify { expect(response).to redirect_to(signin_path) }
end
end
.
.
.
end
end
end
当我试图通过测试时。其中一个失败,消息显示如下:
1) Authentication authorization for non-signed-in users in the Microposts controller
submitting to the destroy action
Failure/Error: before { delete micropost_path(FactoryGirl.create(:micropost)) }
ActiveRecord::RecordInvalid:
Validation failed: User can't be blank
# ./spec/requests/authentication_pages_spec.rb:90:in `block (6 levels) in <top (required)>'
问题是我停止学习ROR一段时间,所以我不知道我在哪里做错了,为什么测试失败了这条消息。 有人可以帮忙吗?
答案 0 :(得分:1)
我认为你的FactoryGirl.create(:micropost)
无效!您似乎在user
中对pre Micropost
进行了user
更新,但未在工厂中指定FactoryGirl.define do
factory :micropost do
...
user
...
end
end
。
您应该将微博的工厂定义为
Factory.create
或与现有用户调用FactoryGirl.create(:micropost, user: user)
(您在let-block之前定义用户):{{1}}
您应该根据自己的需要选择一种方式(工作流程逻辑)