2011-08-14 6 views
5

Rack :: Testの操作方法を理解できない問題がPOSTにあります。これは、クラスとエラーです: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' 

答えて

4

それはあなたのかもしれあなたの個々のクラスにRack :: Testミックスインを含める必要があります。私はクラスを使用しないRSpecを主に使用しますが、特別な機能を引き出すために特殊なRubyのincludeを使用しています。あなたのHelloWorldTestケースクラスの定義の中にinclude Rack::Test::Methodsを入れてみてください。 Sinatraのtestingには、Rack::Testでテストするための詳細情報があります。

+0

実際、私のテストでミックスインを追加し、いくつかの変数を修正することで修正されました。ありがとう! – ferostar

関連する問題