jeudi 30 avril 2015

Hartl Ruby on Rails Tutorial Chapter 12: Tests for the authorization of the following and followers pages - test passed?

I am following the 3rd edition of Hartl's Ruby on Rails Tutorial. In Chapter 12, 12.2.3 Following and followers pages, Listing 12.24: Tests for the authorization of the following and followers pages should be RED according to the book, but I got the test GREEN for some reason.

test/controllers/users_controller_test.rb

require 'test_helper'

class UsersControllerTest < ActionController::TestCase

  def setup 
     @user = users(:user2)
     @other_user = users(:archer)
  end

  test "should redirect index when not logged in" do
    get :index
    assert_redirected_to login_url
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should redirect edit when not logged in" do
    get :edit, id: @user
    assert_not flash.empty?
    assert_redirected_to login_url
  end

  test "should redirect update when logged in" do
    patch :update, id: @user, user: { name: @user.name, email: @user.email }
    assert_not flash.empty?
    assert_redirected_to login_url
  end

   test "should redirect edit when logged in as wrong user" do
    log_in_as(@other_user)
    get :edit, id: @user
    assert flash.empty?
    assert_redirected_to root_url
   end

   test "should redirect update when logged in as wrong user" do
     log_in_as(@other_user)
     patch :update, id: @user, user: { name: @user.name, email: @user.email }
     assert flash.empty?
     assert_redirected_to root_url
   end

   test "should redirect destroy when not logged in" do
      assert_no_difference 'User.count' do
      delete :destroy, id: @user
      end
    assert_redirected_to login_url
   end

   test "should redirect destroy when logged in as an non-admin" do
     log_in_as(@other_user)
     assert_no_difference 'User.count' do
      delete :destroy, id: @user
     end
     assert_redirected_to root_url
   end

  test "should redirect following when not logged in" do
    get :following, id: @user
    assert_redirected_to login_url
  end

  test "should redirect followers when not logged in" do
    get :followers, id: @user
    assert_redirected_to login_url
  end

end

Bellow is the test result:

@rails-tutorial:~/workspace/sample_app (following-users) $ bundle exec rake TEST=test/controllers/users_controller_test.rb
Started

  8/8: [=============================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.57375s
8 tests, 14 assertions, 0 failures, 0 errors, 0 skips

I noticed that there should be 10 tests with the users_controller_test; however, rake test only tested 8 tests and it looks like the last 2 tests were missing.

Can anyone let me know why?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire