On Thu, Mar 5, 2009 at 3:31 AM, Matt W. firstname.lastname@example.org wrote:
So I have something like this
just wraps rails’ testing framework. I’d much rather be constructing
though - you still need to be able to do some kind of DI if you’re doing
TDD, so that you can swap in a fake dependency, no? DI doesn’t have to mean
you use some fancy framework.
Agreed. I think I reacted to the term DI, about which J.B. Rainsberger
once commented “Oh, you mean parameters.” I totally agree that we need
strategies for introducing doubles.
As Pat said, this is hard when the object you want to test (in this case, a
Rails ActionController) is so damn awkward to instantiate in a test.
One approach would be to change the references to @authenticator in your
controller to call a local #authenticator method, then override this with a
TestController subclass and use that subclass instead in your test.
David, how would you suggest the OP injects his MockAuthenticator? stub out
Authenticator.new? I guess that’s what I mostly do.
That’s what I usually do as well. If you think of it, in this case
Authenticator is the factory, and you’re providing a stub factory with
this approach - something that would require more elaborate measures
in other languages.
Maybe we should make this easier by providing some facility in the
mock framework to express the following in one statement:
@authenticator = stub(‘authenticator’)
Sure, you could make that a one liner:
But I mean something like:
@authenticator = Authenticator.stub!
I don’t think that is the answer - but something that concise would be