1

I'm trying to test my SessionsCotroller and have written the following:

   describe "POST create session" do 
        it "should create a new session" do
            params = { session: { email: '[email protected]', password: 'foobar' } }
            post '/api/signin', params
            response['success'] == true
        end

        ...
    end

And I'm getting the following error:

Failure/Error: post '/api/signin', params
 ActionController::UrlGenerationError:
   No route matches {:action=>"/api/signin", :controller=>"sessions", :session=>{:email=>"[email protected]", :password=>"foobar"}}

What am I doing wrong here?

2
  • is the custom URL in your routes.rb file? Have you tried mocking the requests? (Webmock) Commented Aug 26, 2015 at 15:49
  • Yes, the URL is in routes.rb and I don't know how to mock the requests... Would you be able to give an example? Commented Aug 26, 2015 at 16:14

2 Answers 2

2

Use request specs for the purpose

spec/requests/api_v1_spec.rb

require "rails_helper"
RSpec.describe "Create Session", :type => :request do
  describe "POST create session" do 
    it "should create a new session" do
        params = { session: { email: '[email protected]',password:'foobar' } }
        post '/api/signin', params
        response['success'] == true
    end
  end
end

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

2

The methods get and post are described in the Guide to Testing Rails Applications under "Functional Tests for Your Controllers". They take these arguments:

  • The action of the controller you are requesting. This can be in the form of a string or a symbol.
  • An optional hash of request parameters to pass into the action (eg. query string parameters or article variables).
  • An optional hash of session variables to pass along with the request.
  • An optional hash of flash values.

params and session are separate hashes, e.g.:

session = {token: "string"}
params = {thing_id: "123"}

post :action, params, session

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.