require 'spec_helper' describe AuthStrategist::Authorize do let(:authorize) { FactoryGirl.build(:authorize, dummy_component: :ok) } let(:authorize_blank_strategy) { FactoryGirl.build(:authorize_blank_strategy) } let(:authorize_missing_strategy) { FactoryGirl.build(:authorize_missing_strategy) } describe '.call' do context 'when :strategy key was absent in given options' do it 'raises an error' do expect { described_class.call }.to raise_error(StandardError) end end context 'when strategy registered under :strategy key was not found' do let(:missing_strategy) { :missing_strategy } it 'raises an error' do expect { described_class.call(strategy: missing_strategy) }.to raise_error(StandardError) end end context 'when strategy was found' do let(:options) { Hash[:strategy, :dummy_strategy] } before(:each) do allow_any_instance_of(described_class).to receive(:call).and_return(:ok) end it 'builds new service object and sends #call to it' do expect(described_class.call(options)).to eq(:ok) end end end describe '.new' do context 'when :strategy key was absent in given options' do it 'raises an error' do expect { authorize_blank_strategy }.to raise_error(StandardError) end end context 'when strategy registered under :strategy key was not found' do it 'raises an error' do expect { authorize_missing_strategy }.to raise_error(StandardError) end end context 'when strategy was found' do let(:strategy_class) { DummyStrategy } it 'sets strategy up' do expect(authorize.strategy).to be_kind_of(strategy_class) expect(authorize.strategy.dummy_component).to eq(:ok) end it 'returns service object instance' do expect(authorize).to be_kind_of(AuthStrategist::Authorize) end end end describe '#call' do let(:strategy_class) { DummyStrategy } before(:each) do allow_any_instance_of(strategy_class).to receive(:authorize!).and_return(:ok) end it 'sends #authorize! to strategy' do expect(authorize.call).to eq(:ok) end end end