为什么我的集成测试在应用程序运行时失败了?

时间:2015-07-24 00:57:42

标签: ruby-on-rails testing

我有这种集成测试,可以控制登录用户无法编辑其他用户的数据。 代码主要来自Hartl的RoR教程。 这两个测试失败但是当我测试自己时,应用程序的行为符合预期(我无法打开另一个用户的编辑页面)

当我运行rake时,我得到(第31行和第38行断言flash.empty?):

 FAIL["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 2015-07-24 12:15:07 +0800]
 test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1437711307.70s)
        Failed assertion, no message given.
        test/controllers/users_controller_test.rb:31:in `block in <class:UsersControllerTest>'

 FAIL["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 2015-07-24 12:15:07 +0800]
 test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1437711307.71s)
        Failed assertion, no message given.
        test/controllers/users_controller_test.rb:38:in `block in <class:UsersControllerTest>'

然后,如果我发表评论assert flash.empty?,我会得到这个(第32行和第39行是assert_redirected_to root_url):

 FAIL["test_should_redirect_update_when_logged_in_as_wrong_user", UsersControllerTest, 2015-07-24 12:15:08 +0800]
 test_should_redirect_update_when_logged_in_as_wrong_user#UsersControllerTest (1437711308.02s)
        Expected response to be a redirect to <http://test.host/> but was a redirect to <http://test.host/login>.
        Expected "http://test.host/" to be === "http://test.host/login".
        test/controllers/users_controller_test.rb:39:in `block in <class:UsersControllerTest>'

 FAIL["test_should_redirect_edit_when_logged_in_as_wrong_user", UsersControllerTest, 2015-07-24 12:15:08 +0800]
 test_should_redirect_edit_when_logged_in_as_wrong_user#UsersControllerTest (1437711308.03s)
        Expected response to be a redirect to <http://test.host/> but was a redirect to <http://test.host/login>.
        Expected "http://test.host/" to be === "http://test.host/login".
        test/controllers/users_controller_test.rb:32:in `block in <class:UsersControllerTest>'

users_controller_test.rb

require 'test_helper'

class UsersControllerTest < ActionController::TestCase

    def setup
        @user = users(:guillaume)
        @other_user = users(:peter)
    end

    ## other tests ##

  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

失败的行是assert和assert_redirect行

test_helper.rb中

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/reporters'
Minitest::Reporters.use!

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Returns true if a test user is logged in
  def is_logged_in?
    !session[:user_id].nil?
  end

  # Logs in a test user
  def log_in_as(user, options = {})
    password = options[:password]   ||  'password'
    if integration_test?
        post login_path, session: {email: user.email,
                                   password: password}
    else
        session[:user] = user.id 
    end
  end

  private

  # Return true inside an integration test
  def integration_test?
    defined?(post_via_redirect)
  end

end

users_controller.rb

class UsersController < ApplicationController

    before_action :logged_in_user,  only: [:edit, :update]
    before_action :correct_user,    only: [:edit, :update]

    ## new and create methods here ##

    def edit
        @user = User.find(params[:id])
    end

    def update
        if @user.update_attributes(user_params)
            flash[:success] = "Profile updated"
            redirect_to @user
        else
            render 'edit'
        end
    end

    private

        def user_params
            params.require(:user).permit(:name, 
                                         :email,
                                         :club_id,
                                         :access_key, 
                                         :password,
                                         :password_confirmation)
        end

        ## Before filters ##

        # Confirms a logged-in user
        def logged_in_user
            unless logged_in?
                flash[:danger] = "Please log in."
                redirect_to login_url
            end
        end

        # Confirms a correct user
        def correct_user
            @user = User.find(params[:id])
            redirect_to(root_url) unless current_user?(@user)
        end
end

users.yml里

guillaume:
    name: Guillaume
    email: guillaume@example.com
    password_digest: <%= User.digest('password') %>

peter:
    name: Peter
    email: peter@example.com
    password_digest: <%= User.digest('password') %>

我经历了几次代码,但我找不到什么。 这也是我尝试自己开发的第一个应用程序,因此对于本部分,我使用的是Hartl的rails教程资料。

0 个答案:

没有答案