Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to POST data in Rack::Test

Tags:

ruby

sinatra

rack

i'm having problems understanding how to work with Rack::Test, the issue i have is with POST. This are the classes and the error:

hellotesting.rb

require 'sinatra'

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

This is the test:

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

And the output:

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'
like image 866
ferostar Avatar asked Oct 11 '22 06:10

ferostar


1 Answers

It may be that you need to include the Rack::Test mixins into your individual classes. I mainly use RSpec, which doesn't use classes, but does use a specialized variant of Ruby's include for pulling in extra functionality. You may want to try putting in include Rack::Test::Methods inside your HelloWorldTest case class definition. Sinatra's testing has more information for testing with Rack::Test.

like image 170
dirk Avatar answered Oct 12 '22 23:10

dirk