看起来要么我错过了一些非常基本的东西,要么 Rack/Test 无法应对 Sinatra 进行重定向。
大概有办法解决这个问题,否则 Rack/Test 将毫无用处。谁能告诉我我应该怎么做才能让它在这里工作?
(编辑:我最终想要实现的是测试我最终得到哪个页面,而不是状态。在测试中,last_response 对象指向我的应用程序中不存在的页面,当然不是你实际的页面运行时获取。)
一个示例应用程序:
require 'sinatra'
require 'haml'
get "/" do
redirect to('/test')
end
get '/test' do
haml :test
end
这如你所料。转到'/' 或'/test' 可以获得views/test.haml 的内容。
但是这个测试不起作用:
require_relative '../app.rb'
require 'rspec'
require 'rack/test'
describe "test" do
include Rack::Test::Methods
def app
Sinatra::Application
end
it "tests" do
get '/'
expect(last_response.status).to eq(200)
end
end
这是运行测试时发生的情况:
1) test tests
Failure/Error: expect(last_response.status).to eq(200)
expected: 200
got: 302
这last_response.inspect
看起来像:
#<Rack::MockResponse:0x000000035d0838 @original_headers={"Content-Type"=>"text/html;charset=utf-8", "Location"=>"http://example.org/test", "Content-Length"=>"0", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "X-Frame-Options"=>"SAMEORIGIN"}, @errors="", @body_string=nil, @status=302, @header={"Content-Type"=>"text/html;charset=utf-8", "Location"=>"http://example.org/test", "Content-Length"=>"0", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "X-Frame-Options"=>"SAMEORIGIN"}, @chunked=false, @writer=#<Proc:0x000000035cfeb0@/home/jonea/.rvm/gems/ruby-1.9.3-p547@sandbox/gems/rack-1.5.2/lib/rack/response.rb:27 (lambda)>, @block=nil, @length=0, @body=[]>
我想知道 Rack/Test 是否刚刚任意决定在重定向中插入“ http://example.org ”?