0

I've got UserController:

  def update
    if check_token?
      @user.update(user_params)
      head :no_content
    end
  end

I need to test it with 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

I can't make update request properly in the before section. Guys, where can I find documentation with clear examples, how to test Rails api with RSpec. I've found a good book about this topic: http://apionrails.icalialabs.com/book/chapter_three#uid96

But don't want to use FactoryGirl and extra gems to generate users, and the code from this book doesn't work.

Error:

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)>'

Changed code a little bit:

  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

Now I've got the error: ActionController::UrlGenerationError: No route matches {:action=>"update", :controller=>"users", :user=>{:id=>1, :email=>"newmail@example.com"}}

Why is that? My routes:

 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
fernal9301
  • 25
  • 1
  • 4

0 Answers0