Rspec doesn't pickup the status code correctly

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 [email protected] 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 [email protected] please."],
‘url’ => ‘myhost/mylogin’
}.to_json
@format.should_receive(:json).and_return(expected_json)
do_post
end

end

Any ideas?

 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

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

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

On Aug 4, 5:16 am, Steve [email protected] 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

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?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs