I’m getting into RSpec more after getting the awesome Peepcode
screencast about it. So far I love it, but coming form Test::Unit with
Rails, I have some concerns about the recommendation to mock models and
database calls in controller and view specs.
Lets say you have a method on your User model called
User.find_activated, and you have models specs for it. Then in your
controller specs you mock it up like so:
@user = mock_model(User)
Everything works great, and specs execute fast due to limited DB access.
What if I then refactor, and decide to rename the find_activated method
to a more descriptive User.find_all_activated ? So I change the model
spec to reflect the new name, then change the method name in the model,
and all specs pass. Awesome.
So I hit my app and it crashes. I get “no method find_activated for
User”. My controller specs are mocking a method that no longer exists
in the real app. As a result, my specs pass, but my app crashes.
This would not have happened without the mocks, as the spec would have
failed since it called a non-existent method.
So, while I get the benefit of mocking (speed and isolation), it seems
like a gateway for passing specs and failing production applications.
Am I wrong? Am I going about mocking wrong?