Testing matchers

0 views
Skip to first unread message

shenry

unread,
Nov 16, 2009, 7:09:56 PM11/16/09
to sinatrarb
Before my plea for help, lemme say how sweet Sinatra is... I've just
started playing with it (~1 month) and already have a Sinatra app
running on heroku, very cool.

Now to reveal my ignorance...

I'm trying to test my app using the syntax:

post '/foo', { :add => { :agent => 'salt', :add_to => 'eye } } do
assert something
end

and I'd like access to the instance variables returned by that matcher
in main.rb, which would be something like:

post '/foo' do
@add = Add.new('solid', params[:add])
erb :add
end

so that I could test like this

post '/foo', { :add => { :agent => 'salt', :add_to => 'eye' } } do
assert @add.result == 'pain'
end

I've only dipped my toe in the pool of Sinatra testing, so please tell
me if there is a better way of going about this. I am doing some
parameter setting within the matcher methods in main.rb, so I'd like
to run the tests as mock calls to the matchers to cover those bases.

Thanks for any help!

shenry

unread,
Nov 17, 2009, 9:48:59 AM11/17/09
to sinatrarb
I think I answered my own question, I ended up doing

it 'works' do
post '/add', :add => default_params
result = last_response.body.match(/<p id=['"]result['"]>([0-9.,]*)
&nbsp;g<\/p>/)[1]
assert result == "1,000"
end

where the regex looks for the contents of the <p> tag in each view
that contains the result.

Is there a better way?

Simon Rozet

unread,
Nov 17, 2009, 10:05:42 AM11/17/09
to sina...@googlegroups.com
On Tue, Nov 17, 2009 at 3:48 PM, shenry <stuar...@gmail.com> wrote:
>
> I think I answered my own question, I ended up doing
>
> it 'works' do
>    post '/add', :add => default_params
>    result = last_response.body.match(/<p id=['"]result['"]>([0-9.,]*)
> &nbsp;g<\/p>/)[1]
>    assert result == "1,000"
> end
>
> where the regex looks for the contents of the <p> tag in each view
> that contains the result.
>
> Is there a better way?

Checkout http://github.com/brynary/webrat

--
Simon Rozet <si...@rozet.name> http://atonie.org

Reply all
Reply to author
Forward
0 new messages