If you use Mocha and RSpec then read this

I like RSpec, but I'm not a huge fan of it's built in mocking framework. So, when I have the choice, I swap it out for Mocha. However, I really miss mock_model. If you haven't used it, mock_model is an RSpec method where you pass in an ActiveRecord object and it stubs out a whole bunch AR magic so you don't have to. This is crazy useful when testing controllers because when you controller has a line like this:
redirect_to(@model)

I don't want to dig through a ton of Rails code to figure out what I need to stub on this model, I just want it to work. But I just found out that Mislav Marohnić has written a plugin that implements mock_model for Mocha -- so now I can get the best of both worlds. You can find it here:
http://github.com/mislav/rspec-rails-mocha

Thanks Mislav.

Comments

Unknown said…
Personally, I use mock_model of my own which just stubs #id method to return some integer (with ability to autogenerate it) and #new_record?.

That's it. And I use real model instance, so I can rely on logic that is already there.

Popular posts from this blog

On Inject, Complexity, and George Carlin

SICP Wasn’t Written for You

I'm Thinking of Putting View Logic into a Model