[rspec-users] Can I construct the controller myself in a controller spec?

Matt Wynne matt at mattwynne.net
Fri Mar 6 03:05:59 EST 2009


On 5 Mar 2009, at 15:24, Ben Mabey wrote:

> David Chelimsky wrote:
>> On Thu, Mar 5, 2009 at 3:31 AM, Matt Wynne <matt at mattwynne.net>  
>> wrote:
>> Maybe we should make this easier by providing some facility in the
>> mock framework to express the following in one statement:
>>
>> @authenticator = stub('authenticator')
>> Authenticator.stub!(:new).and_return(@authenticator)
>>
>> Sure, you could make that a one liner:
>>
>> Authenticator.stub!(:new).and_return(@authenticator =  
>> stub('authenticator')
>>
>> But I mean something like:
>>
>> @authenticator = Authenticator.stub!
>>
>> I don't think *that* is the answer - but something that concise  
>> would be nice.
>>
>> Thoughts?
>>
>> David
>>
>>
> I like the conciseness, but it isn't very clear what it is doing  
> IMO.  Perhaps something a little more intention-revealing like:
>
> @authenticator = Authenticator.stub_new!

I think I must mostly use constructor injection, as I don't really  
seem to have a pattern for this, but if I did I guess it would be  
something like this:

     extend StubbingHelpers

     describe "blah"
       before(:each) do
         @authenticator = mock_new_authenticator( :foo => bar )

which is implemented like

     def stub_new_authenticator( *args )
       result = mock(Authenticator, *args)
       Authenticator.stub!(:new).and_return(result)
       result
     end

I guess having something like that would be nice.


Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com



More information about the rspec-users mailing list