# \[ANN\] Troll - A Rails plugin for making webservice mocking easier

**URL:** <https://www.ruby-forum.com/t/ann-troll-a-rails-plugin-for-making-webservice-mocking-easier/194054>\
**Category:** Rails\
**Created:** [August 29, 2010, 9:52am UTC](https://www.ruby-forum.com/t/ann-troll-a-rails-plugin-for-making-webservice-mocking-easier/194054 "2010-08-29T09:52:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [August 29, 2010, 9:52am UTC](https://www.ruby-forum.com/t/ann-troll-a-rails-plugin-for-making-webservice-mocking-easier/194054/1 "2010-08-29T09:52:11Z")

</div>

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:

1. 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.

2. Matches based on body should be supported and work (I am talking to  
you fakeweb).

3. 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.

4. 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.

5. 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:

1. Works only with Test/Unit currently. Patches welcome for RSpec  
support.
2. Works with Rails 2.3.x only currently, work is underway for Rails3  
support.

Code:

> **[GitHub - gnufied/troll: Tiny ActiveResource mocking plugin and library](https://github.com/gnufied/troll)**
>
> Tiny ActiveResource mocking plugin and library. Contribute to gnufied/troll development by creating an account on GitHub.

–  
Let them talk of their oriental summer climes of everlasting  
conservatories; give me the privilege of making my own summer with my  
own coals.

[http://gnufied.org](http://gnufied.org)
