Clem Rock wrote:> Hello,
> I''m trying to learn Mocha and having a bit of problems how to
> translate some of my tests to the mocha world.
>
> For example - I''m trying to convert this simple test:
>
> [code]
> def test_get_feed_details_w_bad_url
> assert_incomplete_test
> get :feed_widget_details, :k =>
> ''e6efb203a1cdebcf5a7406175924fe9991979f68'', :id =>
artists(:first).id
> assert_response :success
> assert_equal WEB_SERVER +
''/controller/rss/blog/artist_1'',
> assigns(:blog_url)
> assert_include ''<title type="text">No blog
entries</title>'',
> @response.body
> end
> [code]
>
> What would be the mocha form of this?
>
Not quite sure of the underlying implementation, so not sure I''m going
to be much help, but it feels like there''s way too much stuff being
tested here (Jay Fields has been done some great posts on this recently
on his blog: http://blog.jayfields.com/ and uses Mocha in his examples),
and also, possibly, some of the things being tested should be in a model
(though not necessarily an ActiveRecord one)
First of all I''d test all the fetching and parsing of the feed in a
unit
test. Something like:
def test_should_raise_bad_url_error
Net::HTTP.any_instance.expects(:get).returns(Net::HTTPBadRequest.new("1.1","400","Bad
Request")) # assuming your using Net::HTTP to do the fetching
assert_raise(Feed::BadRequestError) { Feed.new(:url =>
"http://some.nonexistant.url")
end
Then, in the functional test, it''s a lot simpler, cleaner, less
brittle, etc
def test_should_show_message_when_bad_feed_url
Feed.expects(:new).raises(Feed::BadRequestError)
get :feed_widget_details, :k =>
''e6efb203a1cdebcf5a7406175924fe9991979f68'', :id =>
artists(:first).id
assert_response :success
assert_select "title", "No blog entries"
end
I''ve not included the testing of the :blog_url instance variable, as
this is presumably set on all request, and should have its own test.
Hope this helps
Chris
--
----------------
http://pushrod.wordpress.com -- old dogs, new tricks
http://autopendium.com -- a place your old car can call home
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---