spec/object_spec.rb in strong_json-0.0.3 vs spec/object_spec.rb in strong_json-0.0.4

- old
+ new

@@ -13,14 +13,22 @@ type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric)) expect(type.coerce(a: 123, b: true)).to eq(a: 123) end - it "rejects prohibited fields" do - type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:prohibited)) + describe "prohibited" do + it "rejects field with any value" do + type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:prohibited)) - expect{ type.coerce(a: 123, b: true) }.to raise_error(StrongJSON::Type::Error) + expect{ type.coerce(a: 123, b: true) }.to raise_error(StrongJSON::Type::Error) + end + + it "accepts if it does not contains the field" do + type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:prohibited)) + + expect(type.coerce(b: true)).to eq({}) + end end it "rejects objects with missing fields" do type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric)) @@ -45,8 +53,18 @@ it "overrides field" do ty2 = type.merge(a: StrongJSON::Type::Base.new(:prohibited)) expect{ ty2.coerce(a: 123) }.to raise_error(StrongJSON::Type::Error) + end + end + + describe "#except" do + let (:type) { StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric), b: StrongJSON::Type::Base.new(:string)) } + + it "return object which ignores given fields but preserve others" do + ty2 = type.except(:a) + + expect(ty2.coerce(a: 123, b: "test")).to eq({ b: "test" }) end end end