[英]Why is Rails not giving a 3XX on redirect and causing my unit tests to fail?
I'm trying out some unit tests for the first time in Ruby on Rails 5.0.1 (designed sites a few times without tests but just starting to understand the value of proper testing...) and writing a sample app to get the hang of things. 我正在Ruby on Rails 5.0.1中首次尝试一些单元测试(设计的站点几次没有进行测试,但只是开始了解适当测试的价值...),并编写了一个示例应用来解决这个问题东西的。 I have written some tests to make sure the routing works correctly. 我已经编写了一些测试来确保路由正常工作。
test/controllers/static_page_controllers_test 测试/控制器/ static_page_controllers_test
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "root should get static_pages home" do
get '/'
assert_redirected_to static_pages_home_url
end
test "static_pages root should get static_pages home" do
get '/static_pages'
assert_redirected_to static_pages_home_url
end
[some other tests]
end
I have written some routing code to redirect from root and static_pages
root to the static_pages
home
embedded ruby html file. 我已经写了一些路由代码,用于从root和static_pages
根目录重定向到static_pages
home
嵌入式ruby html文件。 This code does correctly redirect in the sense that when you point the browser at /static_pages
or /
the static_pages/home.html.erb
page loads. 从将浏览器指向/static_pages
或/
static_pages/home.html.erb
页面加载的意义上来说,这段代码可以正确重定向。
app/config/routes.rb 应用程序/配置/ routes.rb中
Rails.application.routes.draw do
get '/static_pages' => 'static_pages#home'
get 'static_pages/home'
get 'static_pages/help'
get 'static_pages/about'
root 'static_pages#home'
end
When I run my unit tests for redirects they are still failing. 当我运行用于重定向的单元测试时,它们仍然失败。
rails test
# Running:
F
Failure:
StaticPagesControllerTest#test_root_should_get_static_pages_home [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:6]:
Expected response to be a <3XX: redirect>, but was a <200: OK>
bin/rails test test/controllers/static_pages_controller_test.rb:4
..F
Failure:
StaticPagesControllerTest#test_static_pages_root_should_get_static_pages_home [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:11]:
Expected response to be a <3XX: redirect>, but was a <200: OK>
bin/rails test test/controllers/static_pages_controller_test.rb:9
.
Finished in 0.821392s, 6.0872 runs/s, 6.0872 assertions/s.
5 runs, 5 assertions, 2 failures, 0 errors, 0 skips
A quick wget
to the root URL confirms the Rails Puma server is returning HTTP 200 instead of a 300. Do I need to manually alter the HTTP requests themselves or am I handling the redirect wrongly in some manner? 快速到达根URL的wget
确认Rails Puma服务器返回的是HTTP 200而不是300。我是否需要手动更改HTTP请求本身,还是我以某种方式错误地处理了重定向? How best to change this so my unit tests work correctly and the server returns the right HTTP status code? 如何最好地更改此设置,以便我的单元测试正常工作并且服务器返回正确的HTTP状态代码?
Because you are not redirected as in the HTTP protocol slang. 因为您没有像HTTP协议那样被重定向。 You're just using properly defined routes and get a 200 OK. 您只是使用正确定义的路由,并获得200 OK。
The problem is that these are not actual HTTP redirects. 问题是这些不是实际的HTTP重定向。 When you set up your root
within routes.rb
, and then visit "/"
, you are not going to some page first and then being told via some code that you should be redirected to a different page - you're just being taken straight to the root/home page. 当您在routes.rb
设置root
,然后访问"/"
,您不会先进入某个页面,然后通过一些代码被告知您应该重定向到另一个页面-您被直接带走了。到根/主页。
Same goes for when you set up "/static_pages"
to map (not redirect) to "static_pages#home"
. 当您设置"/static_pages"
以映射 (而不重定向)到"static_pages#home"
。 What is happening is that the Rails server knows that when someone goes to "/static_pages"
, the StaticPagesController
is going to respond, specifically, the #home
method. 发生的情况是,Rails服务器知道有人访问"/static_pages"
StaticPagesController
"/static_pages"
, StaticPagesController
将响应,特别是#home
方法。 There is no redirect happening. 没有重定向发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.