1

I'm using RSpec (through the gem rspec-rails) for testing and developing my application. I've tried to "test" a controller and run up against a strange behavior of post and get methods (same for all the others of this kind).

In my route file:

controller :sessions do post '/login', action: :login_create get '/login', action: :login get '/logout', action: :logout end 

At the beginning, I was thinking that post will simulate an http post request at the specified url, so I've wrote in my spec:

describe "POST 'login'" do it "returns http success" do post 'login' response.should be_success response.should render_template 'sessions/login_create' end end 

But this will call the login action, not the login_create, and then the last assert fail. After a lot of googling and experiments, I've changed post 'login' with post :login_create and this actually works! The strange thing is that also if I change post with get, it will continue to work! O_o Isn't this strange? How this methods are intended to work and to be used?

In the Rails API I've not found anything else than the class: ActionController::TestCase::Behavior

1 Answer 1

1

What you're writing is a controller spec, not a request spec. In a controller spec, routes are not consulted at all because rspec invokes controller actions directly.

When you write post 'login', the login specifies the action name, not the URL path.

The correct way to test the login_create action would be to use post :login_create as you've discovered.

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

2 Comments

That's ok, but what is the difference between post, get, put and delete? Is it only a matter of clarity?
It sets the correct request.method when the action is invoked. (Sorry for the late reply!!)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.