Ruby on rails RoR-测试错误-预期<&引用;类别/新&燃气轮机;但是使用<;[]>;

Ruby on rails RoR-测试错误-预期<&引用;类别/新&燃气轮机;但是使用<;[]>;,ruby-on-rails,unit-testing,rendering,Ruby On Rails,Unit Testing,Rendering,我的应用程序测试有问题。它已经存在太久了,我甚至想知道,为什么我要测试它(因为在测试之外,它工作得很完美!) 以下是错误: $rake测试 运行选项:--seed 1855 `#运行: F 失败: CreateCategoriesTest#测试(无效)类别(提交)结果(失败) [/Users/user/Documents/ruby_project/rails_projects/alpha blog/test/integration/create_categories_test.rb:17]: 预

我的应用程序测试有问题。它已经存在太久了,我甚至想知道,为什么我要测试它(因为在测试之外,它工作得很完美!)

以下是错误:

$rake测试 运行选项:--seed 1855

`#运行:

F

失败: CreateCategoriesTest#测试(无效)类别(提交)结果(失败) [/Users/user/Documents/ruby_project/rails_projects/alpha blog/test/integration/create_categories_test.rb:17]: 预期但渲染为

bin/rails测试/集成/创建类别测试。rb:15

F

失败: CreateCategoriesTest#test(测试)获取(新)类别(表格)和(创建)类别 [/Users/user/Documents/ruby_project/rails_projects/alpha blog/test/integration/create_categories_test.rb:7]: 预期但渲染为

bin/rails测试/集成/创建类别测试。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
作者:

至于“无效类别提交导致失败”测试I,替换为:

post categories_path, category: {name: " "}
作者:

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


有人知道发生了什么事吗?提前谢谢

好的,我已经得到了我问题的答案。愚蠢的我,我没有通过这些测试的用户,而且,很明显,由于需要用户,测试无法再正确执行

下面是测试助手文件中的更正:

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
对于创建类别测试:

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
需要“测试助手”
类createCategateTest

抱歉,各位问了这个没用的问题,打扰了

好的,我已经得到了我问题的答案。愚蠢的我,我没有通过这些测试的用户,而且,很明显,由于需要用户,测试无法再正确执行

下面是测试助手文件中的更正:

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
对于创建类别测试:

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
需要“测试助手”
类createCategateTest

抱歉,各位问了这个没用的问题,打扰了

我把
expect
放在
get:index

哪个应该是

获取:索引
expect(response)。要呈现_模板(:index)

我把
expect
放在
get:index

哪个应该是

获取:索引
expect(response)。要呈现_模板(:index)

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
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
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