ruby-on-rails - URI::InvalidURIError: 错误的 URI(不是 URI?)测试 Rails Controller

标签 ruby-on-rails ruby tdd minitest

我得到 URI::InvalidURIError 测试 Rails Home Controller :

require 'test_helper'

class HomeControllerTest < ActionDispatch::IntegrationTest
  test "should get index" do
    get :index
    assert_response :success
  end
end

得到以下错误:

E

Error:
HomeControllerTest#test_should_get_index:
URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80index
    test/controllers/home_controller_test.rb:7:in `block in <class:HomeControllerTest>'

堆栈如下:

Rails 5.0.0.beta3
minitest (5.8.4)

最佳答案

Controller 测试继承自 ActionController::TestCase,而您的测试 继承自 ActionDispatch::IntegrationTest。所以你使用的是集成测试而不是 Controller 测试。

错误是:

http://www.example.com:80index

这看起来不对,是吗? ;-)

解决方案是使用完整路径:

get '/index'

请记住,集成测试并不真正依赖于任何特定的 Controller (或其他任何东西)。他们测试应用程序中多个组件的集成。因此,如果您要测试 UserControllerindex 操作,您可能需要使用 /users/index

如果您打算进行 Controller 测试而不是集成测试,则需要设置正确的父类(super class)。使用 get :index(对于 index 方法)应该可以正常工作。

关于ruby-on-rails - URI::InvalidURIError: 错误的 URI(不是 URI?)测试 Rails Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35925504/

相关文章:

ruby-on-rails - 分页、随机搜索结果,不聚集

unit-testing - 单元测试。 .NET 4.0 中有哪些新技术/工具?

java - 测试驱动开发、单元测试

ruby - 在 Ruby 中替代救援?

Ruby 运算符重载

ruby-on-rails - Ruby - 如何加速 ".each"数组的循环?

ruby - 使用 Rspec codeschool 3 级挑战 5 进行测试

ruby-on-rails - 如何将变量插入到 rails 链接中的 href 值中?

ruby-on-rails - 给定一个传递给 Nokogiri 的缩短的 URL,如何获得完整的 URL?

ruby-on-rails - 重复的数据库条目