运行minitest时出错,期待<“admin / dashboard”>但使用< []>进行渲染

时间:2018-01-02 06:43:36

标签: ruby-on-rails ruby-on-rails-5 integration-testing

测试此代码时出现故障... 声明的错误是“期待<”admin / dashboard“>但使用< []> 进行渲染”

集成测试页面代码段是

ASBW_HOME/bin

route.rb中的代码段是

require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
  def setup
    @admin = admins(:admn)
  end

  test "admin dashboard link" do
    get login_admin_path
    post login_admin_path, params: { session: { username: @admin.username,
                                          password: 'Ha66y@Air' } }        

    get admin_dashboard_path
    assert_template 'admin/dashboard'
 end
end 

登录控制器中的代码段是

get '/admin/dashboard', to: 'admin#dashboard'

管理控制器中的代码段是

def create
    admin = Admin.find_by(username: params[:session][:username])
    if admin && admin.authenticate(params[:session][:password])
      # render 'admin/dashboard'
      log_in admin
      params[:session][:remember_me] == '1' ? remember(admin) : forget(admin)

      redirect_to admin_dashboard_path


    else
      render 'login/admin'
      flash.now[:danger] = 'Invalid email/password combination'
    end

  end

admins.yml的代码段是

def dashboard
    redirect_to root_url unless logged_in?
    @admin = current_user
  end

该应用在服务器中成功运行。但是,运行测试时会发生错误。

1 个答案:

答案 0 :(得分:1)

redirect_to admin_dashboard_path不呈现模板,它呈现重定向命令; HTTP 302.然后浏览器立即获取指定的页面,这就是手动测试通过的原因。试试assert_redirected_to