[rspec-users] Using mocks

David Chelimsky dchelimsky at gmail.com
Wed May 30 19:45:58 EDT 2007


On 5/30/07, barsalou <barjunk at attglobal.net> 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

These need to be instance variables:

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

See if that works.

David
>
>
>   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
>


More information about the rspec-users mailing list