Hi Folks,
Troll trumps your existing ActiveResource mocking libraries like
inbuilt HttpMock,Fakeweb or Webmock out of water. With Troll,
ActiveResource mocking just works, some of its features:
-
Don’t need to mock calls at Net::HTTP level (which webmock,fakeweb
do). Really, we don’t care what HTTP library ActiveResource uses,
fakeweb and webmock bring additional complexity because of plumbing at
that level. -
Matches based on body should be supported and work (I am talking to
you fakeweb). -
How many times a particular mock can be matched should be
supported. Specifying ‘-1’ as times will match the mock forevever,
unless
another mock is inserted after this one. -
If no match is found or if number of times the mock should be used
has exceeded the predefined count,actual webservice call should be
made. This has been my pet peeve with WebMock and inbuilt httpmock,
they insist on taking over entire stack. Give us a break, at times we
WANT an actual webservice call made, that makes debugging easier. -
The most recent mock should be applied first.
An example to make things clearer:
Including the Module in ActiveSupport::TestCase (test_helper.rb),
class ActiveSupport::TestCase
include Troll::TestUnitStuff
end
and setup block of your tests:
http_mock(:post, “/articles.xml”,{:times => 1,:body => /frodo/},{:body
=> fixture_file(‘article_1.xml’), :status => 201})
More can be found in test directory of plugin and README.
Gotchas:
- Works only with Test/Unit currently. Patches welcome for RSpec
support. - Works with Rails 2.3.x only currently, work is underway for Rails3
support.
Code:
–
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.