Forum: RSpec Just a quick question on David's new-controller-examples

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
17d2c907d8bff4d5f78da74bc4d38466?d=identicon&s=25 Shane Mingins (smingins)
on 2008-10-15 05:09
(Received via mailing list)
http://blog.davidchelimsky.net/2008/7/1/new-contro...

BTW .... great post, thanks :-)

I was wondering the thinking of this one ....

describe "responding to POST /accounts" do

   describe "with failed save" do

       it "should create a new account" do
         Account.should_receive(:new).with({'these' =>
'params'}).and_return(mock_account(:save => false))
         post :create, :account => {:these => 'params'}
       end


compared to the similar example in describe "with successful save".

The expected behaviour seems the same and I wondered why it would not
be pulled into just the responding to POST /accounts block?

Cheers
Shane


Shane Mingins
ELC Technologies (TM)
1921 State Street
Santa Barbara, CA 93101


Phone: +64 4 568 6684
Mobile: +64 21 435 586
Email:  smingins@elctech.com
AIM:     ShaneMingins
Skype: shane.mingins

(866) 863-7365 Tel - Santa Barbara Office
(866) 893-1902 Fax - Santa Barbara Office

+44 020 7504 1346 Tel - London Office
+44 020 7504 1347 Fax - London Office

http://www.elctech.com
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2008-10-15 19:26
(Received via mailing list)
I have a different question about the article (pity comments are
closed).

def mock_account(stubs={})
    stubs = {
      :save => true,
      :update_attributes => true,
      :destroy => true,
      :to_xml => ''
    }.merge(stubs)
    @mock_account ||= mock_model(Account, stubs)
  end


With this helper, doesn't @mock_account only get set once inside its
'describe' block? Any future tests would use the same @mock_account,
even if
other stubs were designated.

I'm probably missing something very obvious.

///ark
17d2c907d8bff4d5f78da74bc4d38466?d=identicon&s=25 Shane Mingins (smingins)
on 2008-10-15 19:59
(Received via mailing list)
Hi Mark

Yeah I was looking at that later on too.  And yes if you use in a
'before' it does.  Only setting in an 'it' block is ok.

I was using this as a quick test as we are on an older version of
rspec and I was checking if it may have changed but this fails on 1.1.8

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe PersonController do

   def mock_person(stubs={})
     @person ||= mock_model(Person, stubs)
   end

   describe "GET" do

     before(:each) do
       mock_person(:name => "fred")
     end

     it "should have name fred" do
       @person.name.should == "fred"
     end

     it "should have name steve" do
       mock_person(:name => "steve")
       @person.name.should == "steve"
     end

   end

   describe "POST" do

     it "should have name steve" do
       mock_person(:name => "steve")
       @person.name.should == "steve"
     end

     it "should have name bob" do
       mock_person(:name => "bob")
       @person.name.should == "bob"
     end

   end

end



1)
'PersonController GET should have name steve' FAILED
expected: "steve",
      got: "fred" (using ==)
./spec/controllers/person_controller_spec.rb:21:

Finished in 0.295898 seconds

6 examples, 1 failure






On 16/10/2008, at 6:26 AM, Mark Wilden wrote:

>     }.merge(stubs)
> ///ark
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Shane Mingins
ELC Technologies (TM)
1921 State Street
Santa Barbara, CA 93101


Phone: +64 4 568 6684
Mobile: +64 21 435 586
Email:  smingins@elctech.com
AIM:     ShaneMingins
Skype: shane.mingins

(866) 863-7365 Tel - Santa Barbara Office
(866) 893-1902 Fax - Santa Barbara Office

+44 020 7504 1346 Tel - London Office
+44 020 7504 1347 Fax - London Office

http://www.elctech.com
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-10-17 00:44
(Received via mailing list)
On Wed, Oct 15, 2008 at 3:58 PM, Shane Mingins <smingins@elctech.com>
wrote:
> describe PersonController do
>
>
>    end
>     got: "fred" (using ==)
> ./spec/controllers/person_controller_spec.rb:21:
>
> Finished in 0.295898 seconds
>
> 6 examples, 1 failure

Admittedly, this is a violation of the principal of least surprise,
but your analysis of the problem is not quite accurate.

Each example is run in its own instance of the group (just like xUnit
frameworks) and has its own state. before(:each) gets run, as it says,
before each example in the context of its own instance of the group.

So what's happening is that the mock_person(:name => "fred") in the
before is creating the instance, whereas the mock_person(:name =>
"steve") in the example is not actually creating a new object - it
essentially does nothing :(

I think that the mock_person method probably needs to be smarter so if
it's already been called AND it gets args it raises an error. WDYT?
This topic is locked and can not be replied to.