2008/12/26 Dieter Komendera <dieter.k...@gmail.com>
Sorry for the slow response. I haven't had much free time over the holiday period.
Like Duncan, I too have reservations about stubbing long chains of named scopes. I would favour writing explicit composite scopes which express the purpose of the scope in as domain-specific language as possible i.e. not necessarily just combining the names of the scopes with the word "and".
One danger of stubbing such long chains of named scopes is that the order of the scopes is not important in terms of the result, but it is important in how the various stubs are set up. This makes the test more brittle and tied to the implementation details of the code e.g. calling account.users.registered.having_email('j...@bloggs.com
') gives the same result as calling account.users.having_email('j...@bloggs.com
').registered, but only one combination would be covered by an invocation of stub_path e.g. account.stub_path("users.activated.find_by_email") and the code might call the other combination. This problem goes away if you use a composite scope.
In general mocking and stubbing doesn't tend to work as well when you move away from a classic OO style towards a more DSL-like style, as Rails has done in this and other cases.
I disagree with you that changing application code to make testing easier is *necessarily* a bad thing. Often the fact that testing some application code is hard is an indicator of a code smell .
Having said all that, although I've always been wary about introducing features into Mocha that might encourage people to get themselves in a mess, this seems like an example where it might be sensible to be pragmatic. Another example of this is Brian Takita's method interception functionality .
I've also tried not to introduce anything Rails-specific into Mocha, to ensure that it's useful to Ruby developers outside Rails and to avoid a maintenance nightmare. However, a few recent requests seem very reasonable e.g. the introduction of mock_model .
I'm starting to think the best way to provide for these requests is to have one or more optionally required bits of Mocha (much as Ken Collins suggested in ).
What do people think?