[rspec-users] Rspec doesn't pickup the status code correctly.

J2M james2mccarthy at gmail.com
Sun Aug 3 15:50:29 EDT 2008


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?


More information about the rspec-users mailing list