Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing a controller method with Sinatra and rspec

So I'm trying to figure out a way of stubbing a controller method in rspec for a Sinatra app. The main reason for this is to test the logical flow of the application and to make sure it calls the necessary functions when certain conditions are met. So, in essence, I want to be able to do something like

controller.should_receive(:fancy_method).and_return("This is a string")

What I'm having difficulty doing is accessing the controller instance within the sinatra app. I am able to override the current functions using a class_eval on the sinatra controller class, but I'd love to assert that these functions actually run.

Anyone have any advice?

Thanks.

like image 594
Dan Frade Avatar asked Dec 11 '09 16:12

Dan Frade


1 Answers

Dan, I believe what you really want is to just test the controller actions. From a tester's perspective you shouldn't really care about what it actually called but rather for the output, given a specific input and maybe some other special conditions (that is mocking or stubbing other classes) (1).

You can check the official documentation for Sinatra + Rack::Test or this blog post from devver.net.

(1) : If your controller pages are calling some other classes (models, services, etc) you could mock these instead and put expectations on them. For example :

SomeClass.should_receive(:msg).with(:arg).and_return(:special_value)

Some more info for mocking (with RSpec in this exmaple) can be found on the RSpec documentation pages.

like image 68
Nikos D Avatar answered Sep 20 '22 13:09

Nikos D