Sha256: 7f77aadeefe71422af45ad1a2aa7ee3e9e9694b531eee72843c8b51b8999d013

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

require "spec_helper"

describe Simple::Service do
  describe ".with_context" do
    it "merges the current context for the duration of the block" do
      block_called = false

      Simple::Service.with_context(a: "a") do
        expect(Simple::Service.context.a).to eq("a")

        # overwrite value
        Simple::Service.with_context(a: "b") do
          expect(Simple::Service.context.a).to eq("b")
          block_called = true
        end

        # overwrite value w/nil
        Simple::Service.with_context(a: nil) do
          expect(Simple::Service.context.a).to be_nil
          Simple::Service.context.a = "c"
          expect(Simple::Service.context.a).to eq("c")
        end
        expect(Simple::Service.context.a).to eq("a")
      end

      expect(block_called).to eq(true)
    end
  end
end

describe Simple::Service::Context do
  let(:context) { Simple::Service::Context.new }

  before do
    context.one = 1
  end

  describe "invalid identifier" do
    it "raises an error" do
      expect { context.one! }.to raise_error(NoMethodError)
    end
  end

  describe "context reading" do
    it "returns a value if set" do
      expect(context.one).to eq(1)
    end

    it "returns nil if not set" do
      expect(context.two).to be_nil
    end
  end

  describe "context writing" do
    it "sets a value if it does not exist yet" do
      context.two = 2
      expect(context.two).to eq(2)
    end

    it "raises a ReadOnly error if the value exists and is not equal" do
      expect { context.one = 2 }.to raise_error(::Simple::Service::ContextReadOnlyError)
    end

    it "sets the value if it exists and is equal" do
      context.one = 1
      expect(context.one).to eq(1)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
simple-service-0.1.6 spec/simple/service/context_spec.rb
simple-service-0.1.5 spec/simple/service/context_spec.rb
simple-service-0.1.4 spec/simple/service/context_spec.rb
simple-service-0.1.3 spec/simple/service/context_spec.rb
simple-service-0.1.2 spec/simple/service/context_spec.rb