0

I have testing code as follow, why "test "should get create" do" always failed????

 # called before every single test def setup logger.debug '2' @new_user = User.create(:email => '[email protected]', :password => 'secret', :password_confirmation => 'secret') logger.debug "==========> #{@new_user.id}" end # called after every single test def teardown @new_user.delete end test "should get create" do logger.debug '1' get :create, :email => "[email protected]", :password => "secret" assert_response :success # <------why it always failed here? assert_redirected_to user_path(@new_user) end 

Console output: Finished in 3.834383 seconds.

 1) Failure: test_should_get_create(SessionsControllerTest) [test/functional/sessions_controller_test.rb:24]: Expected block to return true value. 1 tests, 1 assertions, 1 failures, 0 errors, 0 skips test.log: 2 SQL (2.0ms) SELECT 1 FROM "users" WHERE ("users"."email" = '[email protected]') LIMIT 1 SQL (0.0ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' AREL (6.0ms) INSERT INTO "users" ("email", "hashed_password", "salt", "created_at", "updated_at") VALUES ('[email protected]', 'b4e991c44d9738effa3 98e97d7ed1e6ccad19c90ce2e911344a21bf9c82f915f', '258003960.04544115923816805', '2011-04-21 07:04:00.891929', '2011-04-21 07:04:00.891929') ==========> 980190963 1 Processing by SessionsController#create as HTML Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]"} User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 Redirected to http://test.host/users/980190963 Completed 302 Found in 166ms AREL (0.0ms) DELETE FROM "users" WHERE "users"."id" = 980190963 

This is the function i am testing:

 def create if user = User.authenticate(params[:email], params[:password]) session[:user_id] = user.id redirect_to user_path(user), :notice => 'You have successfully login' else redirect_to login_url, :alert => "Invalid email/password combination" end end 

1 Answer 1

1

Because the response isn't success, the response is a redirect.

Understanding this requires a little understanding of HTTP, and a little understanding of what assert_response :success and redirect_to do. All that assert_response :success is testing is the HTTP response code, it's testing to see whether it's a success code. You get a success code when you render a page back to the browser. When you redirect you do not get a success code, you get a redirect code instead.

So if you want to check the response you could use assert_response :redirect - although this is redundant when you already have the assert_redirected_to

Read more here in the Rails guide for testing

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.