spec/lib/tarquinn/controller_spec.rb in tarquinn-0.2.0 vs spec/lib/tarquinn/controller_spec.rb in tarquinn-0.3.0

- old
+ new

@@ -1,39 +1,112 @@ +# frozen_string_literal: true + require 'spec_helper' describe Tarquinn::Controller do - let(:controller) { Tarquinn::DummyController.new } - let(:subject) { described_class.new(controller) } + subject(:controller) { described_class.new(rails_controller) } + let(:rails_controller) { Tarquinn::DummyController.new } + describe '#call' do - it 'redirects a method call to the controller' do - expect(controller).to receive(:redirect_to) - subject.call(:redirect_to) + context 'when no arguments are given' do + before do + allow(rails_controller).to receive(:redirect_to).with(no_args) + end + + it 'delegate a method call to the controller' do + subject.call(:redirect_to) + expect(rails_controller).to have_received(:redirect_to) + end end + + context 'when arguments are given' do + before do + allow(rails_controller) + .to receive(:redirect_to) + .with(1, 2, opt: 3) + end + + it 'delegate a method call to the controller' do + subject.call(:redirect_to, 1, 2, opt: 3) + + expect(rails_controller).to have_received(:redirect_to) + end + end + + context 'when a block is given' do + let(:block) { proc { 12 } } + + before do + allow(rails_controller) + .to receive(:redirect_to) + .with(no_args).and_yield + end + + it 'delegate a method call to the controller' do + subject.call(:redirect_to, &block) + + expect(rails_controller).to have_received(:redirect_to) + end + + it 'passes the block to be executed' do + expect(subject.call(:redirect_to, &block)).to eq(12) + end + end end + describe '#run' do + context 'when block does not call any controller method' do + let(:block) { proc { 12 } } + + it 'returns the value from the block' do + expect(controller.run(&block)).to eq(12) + end + end + + context 'when block references a value outside' do + let(:block) do + value = 15 + proc { value } + end + + it 'returns the value' do + expect(controller.run(&block)).to eq(15) + end + end + + context 'when block references a method in the controller' do + let(:block) { proc { params } } + + it 'returns the value' do + expect(controller.run(&block)) + .to eq(ActionController::Parameters.new(action: 'show')) + end + end + end + describe '#params' do it 'returns the instance params call' do - expect(subject.params).to eq(action: 'show') + expect(subject.params).to eq(ActionController::Parameters.new(action: 'show')) end end - describe '#has_method?' do + describe '#method?' do context 'when calling for a public method that exists' do it do - expect(subject.has_method?(:parse_request)).to be_truthy + expect(subject.method?(:parse_request)).to be_truthy end end context 'when calling for a private method that exists' do it do - expect(subject.has_method?(:redirection_path)).to be_truthy + expect(subject.method?(:redirection_path)).to be_truthy end end context 'when calling for a non existing method' do it do - expect(subject.has_method?(:non_existing)).to be_falsey + expect(subject.method?(:non_existing)).to be_falsey end end end end