Sha256: 29bdd8103bbd90331f57016967723301d57e3eb799e8943f67a1602a18ae37e0

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

require 'spec_helper'

class MyCustomResponseError < Rapidash::ResponseError
  def errors
    if body.kind_of?(Array)
      body.join(', ')
    else
      body
    end
  end
end

describe Rapidash::ResponseError do

  context "standard response error class" do
    let(:response) { Rapidash::ResponseError.new(env) }
    let(:env) { { :status => '404', :method => 'post', :url => 'http://acme.com/api/posts', :body => 'Page not found' } }

    describe ".initialize" do
      it "should set attributes" do
        expect(response.status).to eq 404
        expect(response.method).to eq 'POST'
        expect(response.url).to eq 'http://acme.com/api/posts'
        expect(response.body).to eq 'Page not found'
      end
    end

    describe "#message" do
      it "should create a formatted error message" do
        message = response.send(:message)
        expect(message).to eq '404 POST http://acme.com/api/posts'
      end
    end
  end

  context "custom error class with #errors method" do
    before :each do
      Rapidash.response_exception_class = MyCustomResponseError
    end

    let(:response) { MyCustomResponseError.new(env) }
    let(:env) { { :status => '404', :method => 'post', :url => 'http://acme.com/api/posts', :body => ['name cannot be blank', 'content cannot be blank'] } }

    it "should call #errors" do
      response.should_receive(:errors)
      response.send(:message)
    end

    it "should create a formatted error message" do
      message = response.send(:message)
      expect(message).to eq '404 POST http://acme.com/api/posts | Errors: name cannot be blank, content cannot be blank'
    end

    after :each do
      Rapidash.response_exception_class = nil
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rapidash-0.3.1 spec/rapidash/response_error_spec.rb
rapidash-0.3.0 spec/rapidash/response_error_spec.rb
rapidash-0.3.0.beta2 spec/rapidash/response_error_spec.rb
rapidash-0.3.0.beta spec/rapidash/response_error_spec.rb