Testing Action Web Service using RSpec

I'm been working on Action Web Service recently, and thought I'd attempt to get the tests working correctly in rspec. Doesn't look like it's documented in the blogosphere, so here goes...

Here's the original sample test:


class BackendControllerApiTest < Test::Unit::TestCase
fixtures :products
def setup
@controller = BackendController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

def test_find_all_products
result = invoke :find_all_products
assert result[0].is_a?(Integer)
end
end

This is fine, but a bit old hat. Let speccify it:


context 'The Product API' do
fixtures :products
controller_name :backend

specify 'should return a number for find_all_products' do
result = invoke :find_all_products
result.class.should_be Integer
end
end

Much nicer. Only it doesn't work as written. Bah.

After much searching, it turns out you need to add the following:


require 'action_web_service/test_invoke'

...to your .rb file, or to spec_helper.rb if you want to ensure that it's included for all specs. Do that and you're away.