views:

27

answers:

1

I'm using Shoulda, Mocha, and Test::Unit for testing. I have the following controller and test:

class FoosController < ApplicationController
  caches_action :show
  def show
    @foo = requested_foo
  end
  private
  def requested_foo
    Foo.find(params[:id])
  end
end


class FoosCachingTest < ActionController::IntegrationTest
  def setup
    @foo = Foo.first
    @session = open_session
  end

  context 'FoosController#show' do
    setup do
      @session.get('/foos/1')
    end

    before_should 'not fetch the Foo from the database' do
      FoosController.any_instance.expects(:requested_foo).never
    end

    before_should 'fetch the Foo from the database' do
      FoosController.any_instance.expects(:requested_foo).once.returns(@foo)
    end
  end
end

How is it that both of those tests could pass? I am not explicitly expiring my mocks at any point. Are Mohca and Shoulda known to interact poorly in this regard?

A: 

Aha! The problem is that the expectations do indeed fail but the errors they throw are swallowed up by the controller's error handling. Testing that the action renders properly makes the proper test fail.

James A. Rosen