如何在 Post#Create 上检查 Rspec 中的现有用户?
How do I check for an existing user in Rspec on a Post#Create?
我正在尝试为我的 InvitationsController#Create
编写测试。
这是一个 POST
http 操作。
基本上应该发生的是,一旦 post#create
首次执行,我们需要做的第一件事是检查系统中是否存在电子邮件的 User
在 Post
请求中通过 params[:email]
传入。
我很难理解我是如何做到这一点的。
我稍后会重构,但首先我想让测试功能正常工作。
这是我的:
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
expect {
post :create, invitation: attributes_for(:member, email: @users.first.email)
}.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: expect {
You must pass an argument rather than a block to use the provided matcher (include "valentin@parisian.org"), or the matcher must implement `supports_block_expectations?`.
# ./spec/controllers/users/invitations_controller_spec.rb:17:in `block (4 levels) in <top (required)>'
我对这个错误并不感到惊讶,因为测试对我来说不合适。如果不在我的 controller#action
.
中编写代码,我无法完全弄清楚如何对此进行测试
我正在使用 FactoryGirl,它工作得很好,因为它 returns 所有数据类型的有效数据。这里的问题是如何让 RSpec 实际测试我需要的功能。
您收到的错误是语法错误,与您的操作应该执行的操作无关。
当您将块 ({}
) 传递给 expect 方法时,您拥有的代码正在被解释。
我会把它改成类似
it 'correctly finds User record of invited user' do
post :create, { email: @users.first[:email] }
expect(response).to include(@users.first[:email])
end
假设创建操作的响应 returns 电子邮件为纯文本,这对我来说似乎很奇怪。
另请注意,我已将 email
直接传递给 post,因为您提到您在 params[:email]
中期待它,但根据您编写的测试,您似乎在期待它params[:invitation][:email]
。
如果是这种情况,请更改该部分。
我正在尝试为我的 InvitationsController#Create
编写测试。
这是一个 POST
http 操作。
基本上应该发生的是,一旦 post#create
首次执行,我们需要做的第一件事是检查系统中是否存在电子邮件的 User
在 Post
请求中通过 params[:email]
传入。
我很难理解我是如何做到这一点的。
我稍后会重构,但首先我想让测试功能正常工作。
这是我的:
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
expect {
post :create, invitation: attributes_for(:member, email: @users.first.email)
}.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: expect {
You must pass an argument rather than a block to use the provided matcher (include "valentin@parisian.org"), or the matcher must implement `supports_block_expectations?`.
# ./spec/controllers/users/invitations_controller_spec.rb:17:in `block (4 levels) in <top (required)>'
我对这个错误并不感到惊讶,因为测试对我来说不合适。如果不在我的 controller#action
.
我正在使用 FactoryGirl,它工作得很好,因为它 returns 所有数据类型的有效数据。这里的问题是如何让 RSpec 实际测试我需要的功能。
您收到的错误是语法错误,与您的操作应该执行的操作无关。
当您将块 ({}
) 传递给 expect 方法时,您拥有的代码正在被解释。
我会把它改成类似
it 'correctly finds User record of invited user' do
post :create, { email: @users.first[:email] }
expect(response).to include(@users.first[:email])
end
假设创建操作的响应 returns 电子邮件为纯文本,这对我来说似乎很奇怪。
另请注意,我已将 email
直接传递给 post,因为您提到您在 params[:email]
中期待它,但根据您编写的测试,您似乎在期待它params[:invitation][:email]
。
如果是这种情况,请更改该部分。