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.
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) > endThese 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 > rubyforge.org/mailman/listinfo/rspec-users >
> 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.
On 5/30/07, barsalou <barjunk at attglobal.net> wrote:> > > 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?Submit a feature request in the tracker: rubyforge.org/tracker/?group_id=797 I tried to set up a docs tracker, but screwed it up and haven''t gotten the rubyforge folks to fix it for me yet ;)> > 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. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > rubyforge.org/mailman/listinfo/rspec-users >
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. --steve 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 > rubyforge.org/mailman/listinfo/rspec-usersSteve Ross sross at calicowebdev.com calicowebdev.com -------------- next part -------------- An HTML attachment was scrubbed... URL: rubyforge.org/pipermail/rspec-users/attachments/20070530/0873f6b4/attachment.html