spec/firebrew/amo_api/search_spec.rb in firebrew-0.1.3 vs spec/firebrew/amo_api/search_spec.rb in firebrew-0.2.0
- old
+ new
@@ -1,38 +1,39 @@
require 'spec_helper'
module Firebrew::AmoApi
describe Firebrew::AmoApi::Search do
before do
- response = File.read("spec/fixtures/amo_api/search/#{self.fixture}")
-
- ActiveResource::HttpMock.respond_to do |mock|
- mock.get Search.path(self.params), {}, response
- end
+ Search.connection = double(:connection)
+ allow(Search.connection).to self.stub
end
after do
- ActiveResource::HttpMock.reset!
+ Search.connection = nil
end
- let(:params){{term: 'hoge'}}
+ let(:stub) do
+ response = File.read("spec/fixtures/amo_api/search/#{self.fixture}")
+ receive(:get).and_return(OpenStruct.new body: response, status: 200)
+ end
+
let(:fixture){'base.xml'}
describe '::fetch(params)' do
- subject{Search.fetch self.params}
+ subject{Search.fetch term: ''}
it { is_expected.to be_instance_of(Array) }
it { expect(subject.size).to eq(3) }
it 'should construct objects' do
- expect(subject[0].id).to eq('100')
+ expect(subject[0].guid).to eq('hoge-ja@example.org')
expect(subject[0].name).to eq('hoge')
- expect(subject[1].id).to eq('101')
+ expect(subject[1].guid).to eq('hoge-fuga-ja@example.org')
expect(subject[1].name).to eq('hoge_fuga')
- expect(subject[2].id).to eq('102')
+ expect(subject[2].guid).to eq('hoge-hoge-ja@example.org')
expect(subject[2].name).to eq('hoge_hoge')
end
context 'when results were empty' do
let(:fixture){'empty.xml'}
@@ -45,25 +46,41 @@
it { is_expected.to be_instance_of(Array) }
it { expect(subject.size).to eq(1) }
it 'should construct objects' do
- expect(subject[0].id).to eq('100')
+ expect(subject[0].guid).to eq('hoge-ja@example.org')
expect(subject[0].name).to eq('hoge')
end
end
+
+ context 'when occurred a network error' do
+ let(:stub) do
+ receive(:get).and_raise(Faraday::ConnectionFailed, 'message')
+ end
+
+ it { expect{subject}.to raise_error(Firebrew::NetworkError, 'Message') }
+ end
+
+ context 'when a response HTTP status was not 200' do
+ let(:stub) do
+ receive(:get).and_return(OpenStruct.new body: '', status: 404)
+ end
+
+ it { expect{subject}.to raise_error(Firebrew::NetworkError, 'Invalid HTTP status: 404') }
+ end
end
describe '::fetch!(params)' do
- subject {Search.fetch! self.params}
+ subject {Search.fetch! term: 'aa'}
it { is_expected.to be_instance_of(Array) }
it { expect(subject.size).to eq(3) }
it { expect{subject}.to_not raise_error }
context 'when results were empty' do
let(:fixture){'empty.xml'}
- it { expect{subject}.to raise_error(Firebrew::ExtensionNotFoundError) }
+ it { expect{subject}.to raise_error(Firebrew::ExtensionNotFoundError, 'Extension not found: like "aa"') }
end
end
end
end