【发布时间】:2015-10-20 06:08:19
【问题描述】:
我正在尝试为我的InvitationsController#Create 编写测试。
这是一个POST http 操作。
基本上应该发生的事情是,一旦post#create第一次执行,我们需要做的第一件事是检查系统中是否存在User,用于通过params[:email]传入的电子邮件在Post 请求上。
我很难弄清楚我是如何做到这一点的。
我稍后会重构,但首先我想让测试功能正常工作。
这就是我所拥有的:
describe 'POST #create' do
context 'when invited user IS an existing user' do
before :each do
@users = [
attributes_for(:user),
attributes_for(:user),
attributes_for(:user)
]
end
it 'correctly finds User record of invited user' do
post :create, { email: @users.first[:email] }
expect(response).to include(@users.first[:email])
end
end
end
这是我得到的错误:
1) Users::InvitationsController POST #create when invited user IS an existing user correctly finds User record of invited user
Failure/Error: post :create, { email: @users.first[:email] }
NoMethodError:
undefined method `name' for nil:NilClass
#@myapp/gems/devise-3.2.4/app/controllers/devise_controller.rb:22:in 'resource_name'
# @myapp/gems/devise_invitable-1.3.6/lib/devise_invitable/controllers/helpers.rb:18:in 'authenticate_inviter!'
# @myapp/gems/devise_invitable-1.3.6/app/controllers/devise/invitations_controller.rb:67:in 'current_inviter'
# @myapp/gems/devise_invitable-1.3.6/app/controllers/devise/invitations_controller.rb:71:in 'has_invitations_left?'
我正在使用 FactoryGirl,它运行良好,因为它返回所有数据类型的有效数据。这里的问题是如何让 RSpec 实际测试我需要的功能。
编辑 1
添加了我的:user工厂:
FactoryGirl.define do
factory :user do
association :family_tree
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
password "password123"
password_confirmation "password123"
bio { Faker::Lorem.paragraph }
invitation_relation { Faker::Lorem.word }
# required if the Devise Confirmable module is used
confirmed_at Time.now
gender 1
end
end
【问题讨论】:
-
也许是个愚蠢的问题,但您是否创建了一个名为
:user的 Rspec 工厂?您可以尝试不使用:each->before do。问题来自您的before。如果:user是FactoryGirl模型,你应该这样称呼它:FactoryGirl.create(:user)。 -
是的,我确实创建了一个名为
:user的工厂。您可以使用一些方法作为速记(例如create, attributes_for, build等)。我在before块中使用create进行了尝试,但我仍然遇到这个问题。 github.com/thoughtbot/factory_girl/blob/master/… -
可以加你
:user工厂吗?在您的devise_controller的第 22 行附近? :) -
我添加了我的
:user工厂。我没有devise_controller。那是 stock devise gem 中的控制器。 -
@bobbystouket - 我认为这是它在
devise_controllergem 中所指的行 - github.com/plataformatec/devise/blob/v3.2/app/controllers/…
标签: ruby-on-rails ruby-on-rails-4 rspec rspec3