[rspec-users] Stopping example execution?

Kyle Hargraves philodespotos at gmail.com
Sat Jun 28 19:24:17 EDT 2008


On Sat, Jun 28, 2008 at 5:57 PM, Britt Mileshosky
<mileshosky at hotmail.com> wrote:
>
> Hello, I'm wondering If I am missing something here when creating an example that sets an expecation at the top or beginning of an action but requires you to stub / mock everything that follows.
>
> Example:
> I want to test that a certain controller is running a before_filter...thats easy:
>
> - controller.should_receive(:require_user)
> - do_get
>
> But now i've got to mock / stub everything else that comes behind this filter so that I don't receive 'unexpected method' errors, or other blowups because I am requesting the whole action.  Is there anyway to stop execution after an expectation has been met? It seems to me that this might clean things up a bit.  Not sure, I'm still fairly new to BDD/Mocking by about 2 weeks.

Yep, you can stub out the requested action on the controller. Say
you're testing that the :index action requires authentication:

  controller.should_not_receive(:index)
  stub_not_logged_in
  do_get

Or the opposite:

  controller.should_receive(:index)
  stub_logged_in
  do_get

Personally I prefer expecting the action instead of expecting the
filters, but I think both would accomplish the same goal, as long as
you tested the filter on its own. If you just wanted to stub out the
action to prevent it from doing anything, you could of course just use
controller.stub!(:index).

HTH

k


More information about the rspec-users mailing list