# Just a quick question on David's new-controller-examples

**URL:** <https://www.ruby-forum.com/t/just-a-quick-question-on-davids-new-controller-examples/150949>\
**Category:** RSpec\
**Created:** [October 15, 2008, 5:09am UTC](https://www.ruby-forum.com/t/just-a-quick-question-on-davids-new-controller-examples/150949 "2008-10-15T05:09:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![codejoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@codejoy](https://www.ruby-forum.com/u/codejoy)\
**Post date:** [October 15, 2008, 5:09am UTC](https://www.ruby-forum.com/t/just-a-quick-question-on-davids-new-controller-examples/150949/1 "2008-10-15T05:09:56Z")

</div>

[http://blog.davidchelimsky.net/2008/7/1/new-controller-examples](http://blog.davidchelimsky.net/2008/7/1/new-controller-examples)

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 M.  
ELC Technologies ™  
1921 State Street  
Santa Barbara, CA 93101

Phone: +64 4 568 6684  
Mobile: +64 21 435 586  
Email: removed\_email\_address@domain.invalid  
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

> **[Elctech.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=elctech.com)**
>
> Get a new domain name for your startup. Quick and professional service. Seamless domain transfers.

---

<div class="post-metadata">

**Author:** ![codejoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@codejoy](https://www.ruby-forum.com/u/codejoy)\
**Post date:** [October 15, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/just-a-quick-question-on-davids-new-controller-examples/150949/2 "2008-10-15T19:26:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![codejoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@codejoy](https://www.ruby-forum.com/u/codejoy)\
**Post date:** [October 15, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/just-a-quick-question-on-davids-new-controller-examples/150949/3 "2008-10-15T19:59:43Z")

</div>

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 W. wrote:

> ```
> }.merge(stubs)
> 
> ```
> 
> ///ark
> 
> * * *
> 
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

Shane M.  
ELC Technologies ™  
1921 State Street  
Santa Barbara, CA 93101

Phone: +64 4 568 6684  
Mobile: +64 21 435 586  
Email: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://www.elctech.com)

---

<div class="post-metadata">

**Author:** ![codejoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@codejoy](https://www.ruby-forum.com/u/codejoy)\
**Post date:** [October 17, 2008, 12:44am UTC](https://www.ruby-forum.com/t/just-a-quick-question-on-davids-new-controller-examples/150949/4 "2008-10-17T00:44:13Z")

</div>

On Wed, Oct 15, 2008 at 3:58 PM, Shane M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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?
