2011-08-14 5 views
5

Rack :: Test를 사용하는 방법을 이해하는 데 문제가 있습니다. POST에 문제가 있습니다.Rack :: Test에서 데이터를 POST하는 방법

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 

그리고 출력 : 이것은 테스트입니다

require 'sinatra' 

post '/foo' do 
    "Hello #{params[:name]}." 
end 

hellotesting.rb :이 클래스와 오류입니다 그것은

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

사실, 내 테스트에 mixins를 추가하고 몇 가지 변수를 수정하여 수정했습니다. 감사! – ferostar

관련 문제