[rspec-users] Using mocks

s.ross cwdinfo at gmail.com
Wed May 30 21:07:33 EDT 2007

Make my_mock an instance variable:

@my_mock = mock('some_name')

And reference it as @my_mock in your examples.

See if that fixes it.


On May 30, 2007, at 3:11 PM, barsalou wrote:

> I've just started doing TDD/BDD and like the idea of mocks.  So I set
> out to use them.  The doc pages seem great, I setup the mock and then
> it just works.  Here is what I am trying to do:
> Myuser is a non-rails model of user attributes.  We are going to be
> mocking the connection to the ldap server.  The user class has a login
> method that connects to the ldap server and if successful, returns the
> user object.  For now though, I'd be happy for it to return the true
> value.
> I'm writing this message cause it doesn't work for me, but I'm sure it
> is because I am not doing something right...but unsure what exactly
> that is.
> describe Myuser do
>   before(:each) do
>     my_mock = mock(:some_name)
>     my_mock.should_receive(:login).with("user","pass").and_return 
> (true)
>   end
>   it "should be logged in" do
>      my_mock.login("user","pass")
>   end
> end
> I get this:
> NameError in 'Myuser should be logged in'
> undefined local variable or method `my_mock' for [Dynamically  
> generated
> class for RSpec example]:#<Class:0xb7633930>
> I figure I'm using the mock wrong in the test, but couldn't find an
> example that made sense to my newby mind.
> Thanks for any direction.
> Mike B.
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Steve Ross
sross at calicowebdev.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070530/0873f6b4/attachment.html 

More information about the rspec-users mailing list