[mocha-developer] Rails functional testing and Mocha

Frederick Cheung fred at 82ask.com
Sun Mar 4 08:16:11 EST 2007


On 4 Mar 2007, at 12:47, David Chelimsky wrote:

> On 3/4/07, Frederick Cheung <fred at 82ask.com> wrote:
>> I've always wanted to be able to do stuff like this in my functional
>> tests
>>
>> c = customers(:customer_1)
>> c.expects(:great_customer_service)
>> post :service_customer, :id => c.id
>
> FWIW, I like to handle this sort of thing like this:
>
> mock_customer = mock
> mock_customer.expects(:great_customer_service)
> Customer.expects(:find).with("37").returns(mock_customer)
> post :service_customer, :id => "37"
>
> This works if your viewpoint is that rails functionals are really
> "controller and view" tests and that they shouldn't depend on real
> models (which is my viewpoint). Even if you think real models should
> be in your functionals, you can do it this way:
>
> customer = customers(:customer_1)
> customer.expects(:great_customer_service)
> Customer.expects(:find).with(customer.id.to_s).returns(c)
> post :service_customer, :id => customer.id

That's neat, definitely a nice approach when it fits in (and I can  
certainly see why you would have the viewpoint you do).

Thanks,

Fred


More information about the mocha-developer mailing list