【发布时间】:2014-10-31 12:47:50
【问题描述】:
看来要么我遗漏了一些非常基本的东西,要么 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”?
【问题讨论】:
标签: ruby sinatra rspec2 rack-test