Module: Cuprum::Rails::RSpec::Contracts::SerializersContracts::ShouldSerializeAttributesContract
- Extended by:
- RSpec::SleepingKingStudios::Contract
- Defined in:
- lib/cuprum/rails/rspec/contracts/serializers_contracts.rb
Overview
Contract specifying that a serializer serializes the expected properties.
Instance Method Summary collapse
-
#apply(example_group, *attribute_names, **attribute_values) ⇒ Object
Adds the contract to the example group.
Instance Method Details
#apply(example_group, *attribute_names, **attribute_values) ⇒ Object
Adds the contract to the example group.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cuprum/rails/rspec/contracts/serializers_contracts.rb', line 26 contract do |*attribute_names, **attribute_values| let(:serializers) do return super() if defined?(super()) Cuprum::Rails::Serializers::Json.default_serializers end let(:context) do return super() if defined?(super()) Cuprum::Rails::Serializers::Context.new(serializers: serializers) end let(:expected_attributes) do tools = SleepingKingStudios::Tools::Toolbelt.instance attribute_names .to_h do |attr_name| [attr_name, context.serialize(object.send(attr_name))] end # rubocop:disable Style/MultilineBlockChain .merge(attribute_values) .then { |hsh| tools.hash_tools.convert_keys_to_strings(hsh) } end let(:serialized) { serializer.call(object, context: context) } it 'should serialize the expected attributes' do expect(serialized.keys).to contain_exactly(*expected_attributes.keys) end attribute_names.each do |attr_name| it "should serialize the #{attr_name.inspect} attribute" do expect(serialized[attr_name.to_s]) .to be == expected_attributes[attr_name.to_s] end end attribute_values.each do |attr_name, attr_value| it "should serialize the #{attr_name.inspect} attribute" do attr_value = instance_exec(&attr_value) if attr_value.is_a?(Proc) expect(serialized[attr_name.to_s]).to be == attr_value end end end |