我在理解如何使用Rack :: Test时遇到问题,我遇到的问题是POST。这是类和错误:
hellotesting.rb
require 'sinatra'
post '/foo' do
"Hello #{params[:name]}."
end
这是测试:
require 'hellotesting'
require 'test/unit'
require 'rack/test'
set :environment, :test
class HelloWorldTest < Test::Unit::TestCase
def test_it_says_hello_to_you
browser = Rack::Test::Session.new(Rack::MockSession.new(Sinatra::Application))
post "/foo", "name" => "Bryan"
assert browser.last_response.ok?
assert_equal 'Hello Bryan', browser.last_response.body
end
end
输出:
1) Error:
test_it_says_hello_to_you(HelloWorldTest):
ArgumentError: wrong number of arguments (1 for 0)
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `name'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `send'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `compile!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `each_pair'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `compile!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1129:in `route'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1118:in `post'
(__DELEGATE__):3:in `send'
(__DELEGATE__):3:in `post'
testingjeison.rb:11:in `test_it_says_hello_to_you'
答案 0 :(得分:4)
您可能需要将Rack :: Test mixins包含在各个类中。我主要使用RSpec,它不使用类,但使用Ruby的include
的专用变体来提取额外的功能。您可能想尝试在include Rack::Test::Methods
案例类定义中加入HelloWorldTest
。 Sinatra的testing提供了更多有关使用Rack::Test
进行测试的信息。