# Rspec doesn't pickup the status code correctly

**URL:** <https://www.ruby-forum.com/t/rspec-doesnt-pickup-the-status-code-correctly/144880>\
**Category:** RSpec\
**Created:** [August 3, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/rspec-doesnt-pickup-the-status-code-correctly/144880 "2008-08-03T21:52:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [August 3, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/rspec-doesnt-pickup-the-status-code-correctly/144880/1 "2008-08-03T21:52:42Z")

</div>

I’m setting the status code on a controller action but rspec doesn’t  
seem to be catching it in my spec. It works in the browser.

This is the controller method, called via xhr with header Accept:  
‘application/json’

def validate  
account = Account.new(:login =\> params[:login])  
account.valid?  
unless (errors = account.errors[‘login’])  
url = [‘http:’,’’, request.host, params[:login]].join(’/’)  
respond\_to do |format|  
format.json { render :json =\> { :url =\> url } }  
end  
else  
url = [‘http:’,’’, request.host].join(’/’)  
respond\_to do |format|  
format.json { render :json =\> {:errors =\> errors, :url =\>  
url }, :status =\> 409}  
end  
end  
end

And the spec;

```
describe "with errors on login" do

  before do
    @account.stub!(:errors).and_return( {
      "password_confirmation"=>["can't be blank"],
      "login"=>["is too short (minimum is 3 characters)", "use

```

only letters, numbers, and .-\_@ please."],  
“password”=\>[“can’t be blank”, “is too short (minimum is 6  
characters)”],  
“email”=\>[“can’t be blank”, “is too short (minimum is 6  
characters)”, “should look like an email address.”]  
} )  
request.env[“HTTP\_HOST”] = “myhost”  
controller.use\_rails\_error\_handling!  
end

```
  it "should return conflict 409" do
    do_post
    response.response_code.should == 409
  end

  it "should only return the login errors & the url" do
    expected_json = {
      "login" => ["is too short (minimum is 3 characters)", "use

```

only letters, numbers, and .-\_@ please."],  
‘url’ =\> ‘myhost/mylogin’  
}.to\_json  
@format.should\_receive(:json).and\_return(expected\_json)  
do\_post  
end

```
end

```

Any ideas?

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [August 3, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/rspec-doesnt-pickup-the-status-code-correctly/144880/2 "2008-08-03T22:20:12Z")

</div>

> ```
> it "should return conflict 409" do
> do_post
> response.response_code.should == 409
> end
> 
> ```

What is the failure message?

Also, it looks like your before block has not set Account.new to  
return @account, and that @account is nil. Is this a nested describe,  
and there’s some other setup happening elsewhere?

Pat

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [August 4, 2008, 1:33am UTC](https://www.ruby-forum.com/t/rspec-doesnt-pickup-the-status-code-correctly/144880/3 "2008-08-04T01:33:48Z")

</div>

It is nested and the rest of the setup is here;

```
before(:each) do
  request.env["HTTP_ACCEPT"] = "application/json"
  @account = mock_account
  @account.stub!(:valid?)
  Account.stub!(:new).and_return(@account)
  @errors = { 'login' => 'Not available, try another!' }
  @account.stub!(:errors).and_return( @errors )
  @json = {:this => 'that'}.to_json
  @format = mock("format", :json => @json )
  controller.stub!(:respond_to).and_yield(@format)
end

```

All the other specs pass the only failure is this being

should return conflict 409  
expected: 409,  
got: 200 (using ==)

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [August 4, 2008, 2:25am UTC](https://www.ruby-forum.com/t/rspec-doesnt-pickup-the-status-code-correctly/144880/4 "2008-08-04T02:25:50Z")

</div>

The failure message is;

should return conflict 409  
expected: 409,  
got: 200 (using ==)

Yes it is nested and the rest of the setup is;

```
before(:each) do
  request.env["HTTP_ACCEPT"] = "application/json"
  @account = mock_account
  @account.stub!(:valid?)
  Account.stub!(:new).and_return(@account)
  @errors = { 'login' => 'Not available, try another!' }
  @account.stub!(:errors).and_return( @errors )
  @json = {:this => 'that'}.to_json
  @format = mock("format", :json => @json )
  controller.stub!(:respond_to).and_yield(@format)
end

```

Thanks.  
James

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [August 4, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/rspec-doesnt-pickup-the-status-code-correctly/144880/5 "2008-08-04T22:10:08Z")

</div>

On Aug 4, 5:16 am, Steve [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > unless (errors = account.errors['login'])
> > end
> > 
> > ```
> > 
> > end
> 
> I don’t know if it’s the cause, but your unless statement is an  
> assignment, and not an equality comparison:
> 
> unless (errors = account.errors[‘login’])

That’s just me being lazy I’m assigning errors I could have put that  
as;

errors = account.errors[‘login’] if account.errors[‘login’]

But it isn’t the cause as it works in the wild, it is just the spec  
that fails.

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [August 5, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/rspec-doesnt-pickup-the-status-code-correctly/144880/6 "2008-08-05T12:31:25Z")

</div>

I’m not having much luck posting here, every other reply seems to go  
astray! So if I double post sorry!

I’m using  
Rails 2.1  
Rspec 1.1.4

Any ideas why this isn’t giving the correct response in Rspec but is  
in the wild?

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [August 4, 2008, 6:22am UTC](https://www.ruby-forum.com/t/rspec-doesnt-pickup-the-status-code-correctly/144880/7 "2008-08-04T06:22:02Z")

</div>

J2M wrote:

> ```
> url = ['http:','', request.host, params[:login]].join('/')
> 
> ```
> 
> end  
> I don’t know if it’s the cause, but your unless statement is an  
> assignment, and not an equality comparison:

unless (errors = account.errors[‘login’])

Steve
