views:

77

answers:

2

I'm getting a failing test here that I'm having trouble understanding. I'm using Test::Unit with Shoulda enhancement. Action in users_controller.rb I'm trying to test...

def create
  unless params[:user][:email] =~ / specific regex needed for this app /i

    # ...

    render :template => 'sessions/new'
  end
end

Test...

context 'on CREATE to :user' do
  context 'with invalid email' do
    setup { post :create, { 'user[email]' => 'abc@abcd' } }
    should_respond_with :success
  end
  # ...
end

Fails because "response to be a <:success>, but was <302>". How is it 302?

Change action to...

def create
  render :template => 'sessions/new'
end

Test still fails.

A: 

If you're using default REST-ful URLs, you probably should use PUT, not POST... Since PUT is connected to create, POST to that URL will give you an unauthorized and redirect.

Ola Bini
Are you sure? In routes Rails generated "POST /users {:controller=>"users", :action=>"create"}" I thought put was for updating. Tried post though. Same test failure.
Ethan
+1  A: 

@Ola: You're wrong: POST is connected to create. PUT is normally connected to update.

A :forbidden is quiet odd though. Here are some suggestions to find the problem (I've never used Shoulda, but I don't think it is a problem with Shoulda.

  1. Make sure the route is defined in config/routes.rb and check with rake routes
  2. Do you have any before_filters that could be responsible for that behaviour (login filter, acts_as_authenticated etc..)? Checkout log/test.log. A halt in the filter chain shows up there.
  3. Print out the response body puts response.body to see what you get returned.

Hope this helps.

Kafka