[rspec-users] Stub a class and instances

Marcelo de Moraes Serpa celoserpa at gmail.com
Mon Apr 5 00:21:25 EDT 2010


For example, I have this scenario where a Directory model will only save if
it can connect to the LDAP server. In a test I'm writting, though,
connecting to the LDAP server is not relevant, so I tried doing this:

 context "LDAP is on" do
        before(:each) do
           Directory.stub!(:authenticate_user).and_return(@user).
          @account.directory = Directory.make :ldap_on
        end
...
end

However, the Directory.make fails (since the before_save ldap test fails,
because authenticate_user can't connect to the LDAP server). The issue is
that it stubs the method as a singleton method of Directory.

One alternative is opening the Directory class and redefining the
authenticate_user method, but I'd like to know if rSpec supports somehow
stubing a class and let it stub instance too.

Cheers,

Marcelo.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100404/aa289312/attachment.html>


More information about the rspec-users mailing list