0

当我尝试使用工厂在我的 Rails 应用程序上测试我的登录时,我不断收到缺少模板的错误。即使我的控制器中有一个重定向,它也需要一个用于我的创建操作的模板。这是我的会话控制器:

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

还有我的gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.8'
gem 'jquery-rails'
gem 'pg'
gem 'heroku'
gem 'taps'
gem 'simple_form'
gem 'bootstrap-sass'
gem 'sorcery'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

group :test do
  gem 'minitest'
  gem 'capybara'
  gem 'capybara_minitest_spec'
  gem 'turn'
  gem 'factory_girl_rails'
end

我的工厂.rb:

FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "foo#{n}@example.com" }
    password "secret"
    password_confirmation "secret"
  end
end

这是我的测试:

需要“test_helper”

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]}.

相反,如果我只是不尝试使用工厂女孩,它可以像这样正常工作:

需要“test_helper”

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

关于可能导致这种情况的任何想法?

4

1 回答 1

1

很明显,当您使用时发生的事情FactoryGirl是您if在控制器中的条件正在评估为假(也许login正在返回nil?)。由于您的重定向仅在条件块内,如果您错过了,那么您将进入默认渲染。所以我不知道你的login方法是做什么的,因此不能告诉你为什么userfalseor nil,但在我看来,这一定是正在发生的事情。尝试在条件子句中为现有视图模板添加redirect或,看看是否可以防止异常。renderelse

于 2012-10-18T17:33:54.633 回答