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

David Chelimsky dchelimsky at gmail.com
Thu Mar 5 01:02:39 EST 2009

On Wed, Mar 4, 2009 at 10:38 PM, Perryn Fowler <pezlists at gmail.com> wrote:
> Hi
> I am experimenting with Constructor based dependency injection for
> rails controllers.
> So I have something like this
> class LoginSessionsController < ApplicationController
>  def initialize(authenticator =  TheRealAuthenticator)
>    @authenticator = authenticator
>  end
>  ....
> end
> The plan was to override the authenticator used when testing with something like
> describe LoginSessionsController.new(MyMockAuthenticator) do
>   ......
> end
> but rspec seems to insist on constructing the controller itself

No, Rails insists upon constructing the controller itself. rspec-rails
just wraps rails' testing framework. I'd much rather be constructing
the objects myself.

As for DI, there is a wealth of discussion about DI in Ruby in various
user lists - general consensus is don't do it. I concur. You don't
need it. You might be particularly interested in
http://rubyconf2008.confreaks.com/recovering-from-enterprise.html, in
which Jamis Buck, who authored a DI library for Ruby named needle,
explains why that was unnecessary.

But even if you disagree, you're kinda stuck here because Rails wants
to construct the controllers for us.


> Is there a way to do this I am missing?
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list