[rspec-users] Using mocks

barsalou barjunk at attglobal.net
Wed May 30 20:44:25 EDT 2007

> Message: 11
> Date: Wed, 30 May 2007 19:45:58 -0400
> From: "David Chelimsky" <dchelimsky at gmail.com>
> Subject: Re: [rspec-users] Using mocks
> To: rspec-users <rspec-users at rubyforge.org>
> Message-ID:
> 	<57c63afe0705301645y15868ecqb8e65803529a3a5e at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 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

That worked for me just fine.  What do I need to do to change the 
examples page to include an example of the usage of the created mock as 
well as mention that those mocks should be instance variables?

I'm assuming they have to be instance variables because they go out of 
scope at the end of before_each?

Thanks for the help.

Mike B.

This message was sent using IMP, the Internet Messaging Program.

More information about the rspec-users mailing list