RoR - 测试错误 - 期待<"类别/新">但使用< []>

时间:2017-06-13 12:03:58

标签: ruby-on-rails unit-testing rendering

我的应用测试存在问题。它现在已经存在了很长时间,我甚至想知道,为什么我要对它进行测试(因为在测试之外,它完美无缺!)??

所以这里有错误:

  

$ rake test   运行选项: - 选择1855

     

`#Running:

     

.F

     

失败:   CreateCategoriesTest#test_invalid_category_submission_results_in_failure   [/Users/user/Documents/ruby_project/rails_projects/alpha-blog/test/integration/create_categories_test.rb:17]:   期待<&#;;类别/新">但使用< []>

进行渲染      

bin / rails test test / integration / create_categories_test.rb:15

     

˚F

     

失败:   CreateCategoriesTest#test_get_new_category_form_and_create_category   [/Users/user/Documents/ruby_project/rails_projects/alpha-blog/test/integration/create_categories_test.rb:7]:   期待<&#;;类别/新">但使用< []>

进行渲染      

bin / rails test test / integration / create_categories_test.rb:5

     

.........

     

以0.638134s结束,18.8048次/秒,25.0731次断言/秒。 12次跑,   16个断言,2个失败,0个错误,0个跳过

测试看起来像:

require 'test_helper'

class CreateCategoriesTest < ActionDispatch::IntegrationTest

  test "get new category form and create category" do
    get new_category_path
    assert_template 'categories/new'
    assert_difference 'Category.count', 1 do
    post categories_path, params: { category: {name: "sports"}}
    follow_redirect!
    end
    assert_template 'categories/index'
    assert_match 'sports', response.body
  end

  test "invalid category submission results in failure" do
    get new_category_path
    assert_template 'categories/new'
    assert_no_difference 'Category.count' do
    post categories_path, params: { category: {name: " "}}
    end
    assert_template 'categories/new'
    assert_select 'h3.panel-title'
    assert_select 'div.panel-body'
  end
end

和控制器:

class CategoriesController < ApplicationController

  before_action :require_admin, except: [:index, :show]

  def index
    @categories = Category.paginate(page: params[:page], per_page: 5)
  end

  def new
    @category = Category.new
  end

  def create
    @category = Category.new(category_params)
    if @category.save
      flash[:success] = "La nueva categoria se creó correctamente"
      redirect_to categories_path
    else 
      render 'new'
    end
  end

  def index
     @categories = Category.paginate(page: params[:page], per_page: 5)
  end

  def show
  end

  private

  def category_params
    params.require(:category).permit(:name)
  end

  def require_admin
    if !logged_in? || logged_in? and !current_user.try(:admin?)
      flash[:danger] = "Solamente el admin puede acceder a esta seccion"
    redirect_to categories_path
    end
  end


end

为了让您了解到目前为止我为解决问题所做的工作(因为它之前出现过),我已经取代了这部分&#34;获取新类别表格并创建类别& #34;:

post_via_redirect categories_path, category: {name: "sports"}

by:

post categories_path, params: { category: {name: "sports"}}
follow_redirect!

对于&#34;无效的类别提交导致失败&#34;我替换了测试:

post categories_path, category: {name: " "}

由:

post categories_path, params: { category: {name: " "}}

并且在我对应用程序(在控制器中)的使用施加限制之前它已经起作用了:

before_action :require_admin, except: [:index, :show]
[...]
  def require_admin
    if !logged_in? || logged_in? and !current_user.try(:admin?)
      flash[:danger] = "Solamente el admin puede acceder a esta seccion"
    redirect_to categories_path
    end
  end

有没有人知道发生了什么?提前致谢!

2 个答案:

答案 0 :(得分:0)

好的,我已经得到了我的问题的答案。愚蠢的我,我没有通过用户进行这些测试,显然,要求用户测试无法正常执行。

所以这是测试帮助文件中的更正:

require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

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

  # Add more helper methods to be used by all tests here...
  def sign_in_as(user, password)
    post login_path, params: { session: {email: user.email, password: password}}
  end
end

和create_categories_test:

require 'test_helper'

class CreateCategoriesTest < ActionDispatch::IntegrationTest

    def setup
        @user = User.create(username: "John", email: "john@example.com", password: "password", admin: true)
    end

  test "get new category form and create category" do
    sign_in_as(@user, "password")
    get new_category_path
    assert_template 'categories/new'
    assert_difference 'Category.count', 1 do
    post categories_path, params: { category: {name: "sports"}}
    follow_redirect!
    end
    assert_template 'categories/index'
    assert_match 'sports', response.body
  end

  test "invalid category submission results in failure" do
    sign_in_as(@user, "password")
    get new_category_path
    assert_template 'categories/new'
    assert_no_difference 'Category.count' do
    post categories_path, params: { category: {name: " "}}
    end
    assert_template 'categories/new'
    assert_select 'h3.panel-title'
    assert_select 'div.panel-body'
  end
end

对不起家伙的无用问题和困扰!

答案 1 :(得分:0)

我在expect

之前放get :index

哪个应该是 get :index expect(response).to render_template(:index)