URI :: InvalidURIError:错误的URI(不是URI?)测试Rails控制器 [英] URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers

查看:157
本文介绍了URI :: InvalidURIError:错误的URI(不是URI?)测试Rails控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到URI::InvalidURIError测试Rails Home控制器:

I get URI::InvalidURIError testing 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)

推荐答案

控制器测试从ActionController::TestCase继承,而您的测试 从ActionDispatch::IntegrationTest继承.因此,您使用的是集成测试,而不是控制器测试.

Controller tests inherit from ActionController::TestCase, while your test inherits from ActionDispatch::IntegrationTest. So you're using an integration test and not a controller test.

错误是:

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

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

That doesn't look right, does it? ;-)

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

get '/index'

请记住,集成测试并没有真正绑定到任何特定的控制器(或其他任何与此相关的控制器).他们测试了应用程序中几个组件的 integration .因此,如果要测试UserControllerindex操作,则可能需要使用/users/index.

Remember, integration tests aren't really tied to any specific controller (or anything else, for that matter). They test the integration of several components in your application. So if you're testing the index action of a UserController you'd probably need to use /users/index.

如果要进行控制器测试而不是集成测试,则要设置正确的超类.然后,使用get :index(用于索引方法)应该可以正常工作.

If you intended to make a controller test and not an integration test, you want to set the correct superclass. Using get :index (for the index method) should work fine then.

这篇关于URI :: InvalidURIError:错误的URI(不是URI?)测试Rails控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆