我正在尝试测试这种重定向到同一页面但使用www的方法。前缀,如果它不存在。 它执行重定向,但RSpec测试返回“预期响应为<:redirect>,但是< 200>”。那是为什么?
application_controller.rb
def check_uri
if request.subdomain.present? and request.subdomain.first != "www"
redirect_to request.protocol + "www." + request.host_with_port + request.fullpath if !/^www/.match(request.host)
end
end
application_controller_spec.rb#
describe :check_uri do
it "should redirect to www" do
{ :get => "http://sub.lvh.me:3000/accounts/sign_up" }.
should redirect_to "http://www.sub.lvh.me:3000/accounts/sign_up"
end
end
当我调试时,我得到: (
rdb:1) p response
#<ActionController::TestResponse:0x0000010277d988 @writer=#<Proc:0x0000010277c678@/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/gems/actionpack-3.0.7/lib/action_dispatch/http/response.rb:43 (lambda)>, @block=nil, @length=0, @header={}, @status=200, @body=[], @cookie=[], @sending_file=false, @blank=false, @cache_control={}, @etag=nil>
答案 0 :(得分:0)
您可以测试回复的标头位置吗?
response.header["Location"].should eq("http://www.sub.lvh.me:3000/accounts/sign_up"
)
答案 1 :(得分:0)
您的测试没有达到您在application_controller.rb中定义的方法check_uri
,因为RSpec使用默认测试主机运行,例如端口80上的“test.host”。
您应该调用应用程序控制器中存在的操作并将请求主机和端口存根,如下所示:
describe :check_uri do
it "should redirect to www" do
@request.host = 'sub.lvh.me'
@request.port = 3000
get :index
expect(response).to redirect_to "http://www.sub.lvh.me:3000/accounts/sign_up"
end
end