spec/simple/service/context_spec.rb in simple-service-0.1.1 vs spec/simple/service/context_spec.rb in simple-service-0.1.2
- old
+ new
@@ -1,33 +1,65 @@
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 "reading" do
+ 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 "writing" do
+ describe "context writing" do
it "sets a value if it does not exist yet" do
- context.two = 2
+ 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::Context::ReadOnlyError)
+ 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)