Sha256: 56ba7ab15044474666573a5efc316d5e0aea92e200aca79472e001ae1c645992

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

RSpec.describe Patterns::Service do
  after { Object.send(:remove_const, :DoSomething) if defined?(DoSomething) }

  describe ".call" do
    it "returns instance of service object" do
      DoSomething = Class.new(Patterns::Service) do
        def call; end
      end

      expect(DoSomething.call).to be_kind_of(DoSomething)
    end

    it "instantiates service object passing arguments to constructor" do
      DoSomething = Class.new(Patterns::Service) do
        def initialize(argument_1:, argument_2:); end
        def call; end
      end

      expect {
        DoSomething.call
      }.to raise_error ArgumentError

      expect {
        DoSomething.call(argument_1: 10, argument_2: 20)
      }.not_to raise_error
    end

    it "calls #call method on service object instance" do
      Spy = Class.new do
        def self.some_method; end
      end
      allow(Spy).to receive(:some_method)
      DoSomething = Class.new(Patterns::Service) do
        def call
          Spy.some_method
        end
      end

      DoSomething.call

      expect(Spy).to have_received(:some_method)
    end

    it "requires #call method to be implemented" do
      DoSomething = Class.new(Patterns::Service)

      expect {
        DoSomething.call
      }.to raise_error NotImplementedError
    end
  end

  describe "#result" do
    it "returns a result of expression within #call method" do
      DoSomething = Class.new(Patterns::Service) do
        def call
          50
        end
      end

      service = DoSomething.call
      expect(service.result).to eq 50
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails-patterns-0.6.0 spec/patterns/service_spec.rb
rails-patterns-0.5.0 spec/patterns/service_spec.rb
rails-patterns-0.4.1 spec/patterns/service_spec.rb
rails-patterns-0.4.0 spec/patterns/service_spec.rb
rails-patterns-0.3.0 spec/patterns/service_spec.rb
rails-patterns-0.2.0 spec/patterns/service_spec.rb