Ruby on rails 尝试使用factory girl在my rails应用程序中测试用户登录时,创建操作上缺少模板错误
当我尝试使用工厂来测试我在rails应用程序上的登录时,我不断收到一个缺少模板的错误。它期望我的创建操作有一个模板,即使我的控制器中有一个重定向。这是我的会话控制器:Ruby on rails 尝试使用factory girl在my rails应用程序中测试用户登录时,创建操作上缺少模板错误,ruby-on-rails,testing,capybara,factory-bot,minitest,Ruby On Rails,Testing,Capybara,Factory Bot,Minitest,当我尝试使用工厂来测试我在rails应用程序上的登录时,我不断收到一个缺少模板的错误。它期望我的创建操作有一个模板,即使我的控制器中有一个重定向。这是我的会话控制器: class SessionsController < ApplicationController def new end def create user = login(params[:email], params[:password], params[:remember_me]) if use
class SessionsController < ApplicationController
def new
end
def create
user = login(params[:email], params[:password], params[:remember_me])
if user
redirect_back_or_to dashboard_path, :success => "Logged in!"
else
flash.now.alert = "Email or password was invalid."
end
end
def destroy
logout
redirect_to root_url, :notice => "Logged out!"
end
end
My Factorys.rb:
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "foo#{n}@example.com" }
password "secret"
password_confirmation "secret"
end
end
这是我的测试:
需要“测试助手”
但当我运行该测试时,会出现以下错误:
Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.
相反,如果我不尝试使用factory girl,它的工作原理如下:
需要“测试助手”
关于可能导致这种情况的原因有什么想法吗?很明显,当您使用
FactoryGirl
时,发生的情况是,如果控制器中的条件评估为false(可能登录
返回nil
?)。由于重定向仅在条件块内,如果没有重定向,则将使用默认渲染。因此,我不知道您的登录
方法是什么,因此无法告诉您为什么用户
是假
或零
,但在我看来,这一定是发生的事情。尝试在条件的else
子句中为现有视图模板添加redirect
或render
,看看这是否可以防止异常。这是有效的,当我将此else redirect\u添加到登录路径时,:alert=>“电子邮件或密码无效。”
结束我的控制器,它解决了问题。
describe "Login integration" do
it "logs in a user successfully" do
user = FactoryGirl.create(:user)
visit login_path
fill_in "Email", :with => user.email
fill_in "Password", :with => user.password
check "Remember me"
click_button "Log in"
current_path == "/dashboard"
page.text.must_include "Logged in!"
page.text.must_include "Your Dashboard"
end
end
Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.
describe "Login integration" do
it "logs in a user successfully" do
visit signup_path
fill_in "Email", :with => "joey@ramones.com"
fill_in "Password", :with => "rockawaybeach"
fill_in "Password confirmation", :with => "rockawaybeach"
click_button "Create User"
current_path == "/"
page.text.must_include "Signed up!"
visit login_path
fill_in "Email", :with => "joey@ramones.com"
fill_in "Password", :with => "rockawaybeach"
check "Remember me"
click_button "Log in"
current_path == "/dashboard"
page.text.must_include "Logged in!"
page.text.must_include "Your Dashboard"
end
end