运行测试`test_should_get_index`时已经定义了Rails错误

时间:2017-09-04 13:57:05

标签: ruby-on-rails integration-testing ruby-on-rails-5 rails-api

我正在使用Rails 5.1.13

当我进行个别测试时,它会通过:

 rails test test/api/controllers/positions_controller_test.rb

但是当我在一个文件夹中运行所有测试时,它会失败:

rails test test/api/controllers/

出现此错误消息: test_should_get_index已在

中定义
Running via Spring preloader in process 14518
/home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/testing/declarative.rb:14:in `test': test_should_get_index is already defined in Api::V1::PositionsControllerTest (RuntimeError)
  from /home/mato/cuadernogordo/test/api/controllers/teachers_controller_test.rb:11:in `<class:PositionsControllerTest>'
  from /home/mato/cuadernogordo/test/api/controllers/teachers_controller_test.rb:3:in `<top (required)>'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:292:in `require'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:292:in `block in require'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:258:in `load_dependency'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:292:in `require'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/railties-5.1.3/lib/rails/test_unit/runner.rb:50:in `block in load_tests'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/railties-5.1.3/lib/rails/test_unit/runner.rb:50:in `each'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/railties-5.1.3/lib/rails/test_unit/runner.rb:50:in `load_tests'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/railties-5.1.3/lib/rails/test_unit/runner.rb:39:in `run'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/railties-5.1.3/lib/rails/commands/test/test_command.rb:38:in `perform'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/railties-5.1.3/lib/rails/command/base.rb:63:in `perform'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/railties-5.1.3/lib/rails/command.rb:44:in `invoke'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/railties-5.1.3/lib/rails/commands.rb:16:in `<top (required)>'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:292:in `require'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:292:in `block in require'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:258:in `load_dependency'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:292:in `require'
  from /home/mato/cuadernogordo/bin/rails:9:in `<top (required)>'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:286:in `load'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:286:in `block in load'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:258:in `load_dependency'
  from /home/mato/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:286:in `load'
  from /home/mato/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
  from /home/mato/.rvm/rubi

当我单独运行时,该文件夹中的所有和每个测试都会通过。

我对控制器的所有测试都或多或少地具有相同的形式,如下所示:

require 'test_helper'

class Api::V1::PositionsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @school = schools(:school_one)
    @teacher = teachers(:teacher_one)
    @course = courses(:course_one)
    @position = positions(:position_one)
  end

  test "should get index" do
    get api_v1_positions_url
    assert_response :success
  end

  test "should create position" do
    assert_difference('Position.count') do
      post "/api/v1/positions", params: { position: { course_id: @course.id, shift: "tarde", status: "activo", teacher_id: @teacher.id, position_type: "titular" }}
    end
    assert_response 201
    assert_response :success
  end

  test "should show position" do
    get api_v1_position_url(@position)
    assert_response :success
  end

  test "should update position" do
    patch api_v1_position_url(@position), params: { position: { shift: "tarde", status: @position.status, teacher_id: @position.teacher_id, position_type: "suplente" } }
    assert_response :success
  end

  test "should destroy position" do
    assert_difference('Position.count', -1) do
      delete api_v1_position_url(@position)
    end
    assert_response :success
  end
end

0 个答案:

没有答案