我有UserController:
def update
if check_token?
@user.update(user_params)
head :no_content
end
end
我需要用Rspec
测试它 describe 'PUT /users/:id' do
let!(:user) { User.create(id: 1, email: 'test@mail.ru', password: 'abc123') }
context '# check success update' do
before(:each) do
patch :update, { user: { id: 1, email: 'newmail@example.com' } }
end
it 'returns status code 204' do
expect(response).to have_http_status(204)
end
end
end #<-- end of PUT /users/:id
我无法在之前的部分正确发出更新请求。伙计们,我在哪里可以找到带有明确示例的文档,如何使用RSpec测试Rails api。我找到了一本关于这个主题的好书: http://apionrails.icalialabs.com/book/chapter_three#uid96
但是不想使用FactoryGirl和额外的宝石来生成用户,而且本书中的代码不起作用。
错误:
Failures:
1) UsersController PUT /users/:id # check success update returns status code 204
Failure/Error: patch :update, { user: { id: 1, email: 'newmail@example.com' } }
ArgumentError:
unknown keyword: user
# ./spec/controllers/users_controller_spec.rb:37:in `block (4 levels) in <top (required)>'
稍微改变了一下代码:
describe 'PUT /users/:id' do
let!(:user) { User.create(id: 1, email: 'test@mail.ru', password: 'abc123') }
context '# check success update' do
before { patch 'update', params: { user: { id: 1, email: 'newmail@example.com' } } }
it 'returns status code 204' do
expect(response).to have_http_status(204)
end
end
end #<-- end of PUT /users/:id
现在我收到了错误: ActionController的:: UrlGenerationError: 没有路由匹配{:action =&gt;“update”,:controller =&gt;“users”,:user =&gt; {:id =&gt; 1,:email =&gt;“newmail@example.com”}} < / p>
为什么? 我的路线:
users GET /users(.:format) users#index
POST /users(.:format) users#create
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy