Sha256: 5a3453c5133b1b0fe8b715e66480b1ad86019f6af7f75bfd9487c4f9bc4433f5

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

RSpec.describe BloodContracts::Core::ExceptionCaught do
  before do
    module Test
      class JsonType < BC::Refined
        prepend BC::ExceptionHandling

        def match
          @context[:json_type_input] = value
          @context[:parsed_json] = JSON.parse(@context[:json_type_input])
          self
        end
      end
    end
  end

  subject { Test::JsonType.match(value) }

  context "when value is valid" do
    let(:value) { '{"some": "thing"}' }
    let(:payload) { { "some" => "thing" } }

    it do
      is_expected.to be_valid
      expect(subject.context[:json_type_input]).to eq(value)
      expect(subject.context[:parsed_json]).to match(payload)
    end
  end

  context "when value is invalid" do
    context "when value is a String" do
      let(:value) { "nope, I'm not a JSON" }

      it do
        is_expected.to be_invalid
        expect(subject.context[:json_type_input]).to eq(value)
        expect(subject.exception).to match(kind_of(JSON::ParserError))
      end
    end

    context "when value is a arbitrary object" do
      let(:value) { Class.new }

      it do
        is_expected.to be_invalid
        expect(subject.context[:json_type_input]).to eq(value)
        expect(subject.exception).to match(kind_of(TypeError))
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
blood_contracts-ext-0.1.4 spec/blood_contracts/ext/exception_caught_spec.rb
blood_contracts-ext-0.1.3 spec/blood_contracts/ext/exception_caught_spec.rb
blood_contracts-ext-0.1.2 spec/blood_contracts/ext/exception_caught_spec.rb
blood_contracts-ext-0.1.1 spec/blood_contracts/ext/exception_caught_spec.rb
blood_contracts-ext-0.1.0 spec/blood_contracts/ext/exception_caught_spec.rb