创建成功后,redirect_to上的Rails集成测试失败

时间:2015-11-11 17:58:20

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

我的Rails 4.2.4应用程序中的集成测试失败,出现此错误:

  

test_create_subject_pathway#SubjectPathwaysTest(1446991554.60s)           期待<" subject / show">但使用< []>进行渲染           test / integration / subject_pathways_test.rb:33:在`block in'

关于flash内容的断言在测试中证明新主题已成功保存。使用撬显示更直接。我无法理解为什么重定向到主题/显示'在考试中失败了。当我在浏览器中遵循此路径时,重定向成功。

谷歌搜索发现了类似的问题,但似乎没有一个直接适用。我的希望是其他的眼睛会发现我的错误。

该应用程序基于Michael Hartl教程,并传递与该教程已完成的应用程序相关的所有测试。该应用程序将跟踪研究对象,以便添加主题模型。

class CreateSubjects < ActiveRecord::Migration
  def change
    create_table :subjects do |t|
      t.string  :first
      t.string  :middle
      t.string  :last
      t.date    :dob
      t.integer :age
      t.string  :gender

      t.timestamps null: false
    end
  end
end

class Subject < ActiveRecord::Base
end

控制器:

class SubjectsController < ApplicationController
  before_action :set_subject, only: [:show, :edit, :update, :destroy]

  def index
    @subjects = Subject.order(last: :asc, 
                              first: :asc, 
                              middle: :asc).page params[:page]
  end

  def show
  end

  def new
    @subject = Subject.new
  end

  def create
    @subject = Subject.new(subject_params)
    if @subject.save
      flash[:success] = "Subject was saved."
      redirect_to @subject
    else
      render :new
    end
  end
  ...
private
  def set_subject
    @subject = Subject.find(params[:id])
  end

  def subject_params
    params.require(:subject).permit(:first, 
      :middle, :last, :dob, :age, :gender)
  end
end

路线:

Rails.application.routes.draw do
  root to: 'static_pages#home'
  match '/home',    to: 'static_pages#home',    via: 'get'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'

  match '/signup',  to: 'users#new',            via: 'get'
  match '/signin',  to: 'sessions#new',         via: 'get'
  match '/signin',  to: 'sessions#create',      via: 'post'
  match '/signout', to: 'sessions#destroy',     via: 'delete'
  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]

  resources :subjects do
  end
end

最后集成测试失败的断言:

require 'test_helper'
class SubjectPathwaysTest < ActionDispatch::IntegrationTest
  def setup
    @user = User.new( name:  'user', 
                      email: 'user@abc.com', 
                      password:              'password', 
                      password_confirmation: 'password',
                      admin: false,
                      activated: true,
                      activated_at: Time.zone.now )
    @user.save!
  end

  test 'create subject pathway' do
    sign_in_as @user
    follow_redirect!
    # follow subject link
    get subjects_path
    assert_template 'subjects/index'
    # insert new subject
    assert_select "a[href=?]", new_subject_path
    get new_subject_path
    assert_template 'subjects/new'
    post subjects_path, subject: { first:  'Joe',
                                   middle: 'T',
                                   last:   'Garcia',
                                   dob:     '',
                                   age:     55,
                                   gender:  'M' }
    assert_not flash.empty?
    assert_equal 'Subject was saved.', flash[:success]
    assert_template 'subjects/show'  # <-- This fails!!
  end
end

test_helper提供了这种方法:

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

1 个答案:

答案 0 :(得分:0)

事实证明解决方案正在盯着我。一个follow_redirect!在assert_template&#39; subject / show&#39;之前是必需的。请参阅下面的代码段。没有follow_redirect!实际上并未呈现模板&#39;。

test 'create subject pathway' do
  ...
  post subjects_path, subject: { first:  'Joe',
                                 middle: 'T',
                                 last:   'Garcia',
                                 dob:     '',
                                 age:     55,
                                 gender:  'M' }
  assert_not flash.empty?
  assert_equal 'Subject was saved.', flash[:success]
  follow_redirect!                 # <-- Added this line
  assert_template 'subjects/show'  # <-- No longer fails