ruby - Sinatra unit test - post with JSON body -
i trying build unit test rest api built using sinatra. right want test echo function works right. echo uses post , return exact same payload post. still new ruby, forgive me if don't use proper lingo.
here code want test:
post '/echo' request.body.read end
this unit test trying make:
env['rack_env'] = 'test' require './rest_server' require 'test/unit' require 'rack/test' require 'json' class restserver < test::unit::testcase def app sinatra::application end def test_check_methods data = '{"datain": "hello"}' response = post '/echo', json.parse(data) assert.last_response.ok? assert(response.body == data) end end
with above code, here error:
nomethoderror: undefined method `datain' sinatra::application:class /users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1285:in `block in compile!' /users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1285:in `each_pair' /users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1285:in `compile!' /users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1267:in `route' /users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1256:in `post' /users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1688:in `block (2 levels) in delegate' /users/barrywilliams/rubymineprojects/project/rest_server_test.rb:20:in `test_check_methods'
if try doing without json.parse
,
nomethoderror: undefined method `key?' "{\"datain\": \"hello\"}":string /users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1265:in `route' /users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1256:in `post' /users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1688:in `block (2 levels) in delegate' /users/barrywilliams/rubymineprojects/project/rest_server_test.rb:20:in `test_check_methods'
if try doing data = 'hello'
, same undefined method 'key?'
error
i've tried suggestion, no success: http://softwareblog.morlok.net/2010/12/18/testing-post-with-racktest/ error saying post
takes 2 arguments, not 3.
so, in summary, need able make call, have code i'm testing receive call , return response, need able read response , verify original data. right looks it's getting stuck @ making call.
thanks, barry
rack test give response body in last_response.body
, no need save variable. you're not echoing you've sent - data
in code you've given json, converted hash , posted that, it's not going match comes back. either send json, or convert json in sinatra route if want (see https://stackoverflow.com/a/12138793/335847 more).
in sinatra app:
require 'json' post '/echo' # don't use request.body.read you're not posting json params.to_json end
and in test file:
def test_check_methods data = '{"datain": "hello"}' post '/echo', json.parse(data) assert.last_response.ok? assert(last_response.body == data) end
if do end wanting post json (which think not idea if it's easy convert or have data hash) use :provides => "json"
condition route, , consider using rack::test::accepts make life easier writing test (note: that's shameless plug gem wrote;)
Comments
Post a Comment