Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat([PoC]): Contract for sequence of messages. #21

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Mifrill
Copy link
Contributor

@Mifrill Mifrill commented Feb 9, 2023

📓 This PR is just a PoC on how to use Pact for a sequence of messages in a single test iteration. However, this approach is opposite to Consumer-driven Contract Testing (CDC), because is test should be as much simple as possible to test only one iteration (request-response) at a time.

When a Consumer expects a few messages in one test iteration current Pact is keeping only the last message in Contract, this modification makes Pact keeps all messages in Contract.

describe TestMessageConsumer do
  let(:expected_payload_1) {
    {
      "first_name": "John",
      "last_name": "Doe"
    }
  }
  let(:expected_payload_2) {
    {
      "hello": "world"
    }
  }
  let(:consumer) { described_class.new }

  describe "Test Message Consumer", pact: :message do
    it "generates contract" do
      test_message_producer
        .given("Class1")
        .is_expected_to_send("created")
        .with_content(expected_payload_1)
      test_message_producer.send_message_string do |content_string|
        expect(consumer.consume_message(content_string)).to eq(expected_payload_1.to_json)
      end

      test_message_producer
        .given("Class2")
        .is_expected_to_send("updated")
        .with_content(expected_payload_2)
      test_message_producer.send_message_string do |content_string|
        expect(consumer.consume_message(content_string)).to eq(expected_payload_2.to_json)
      end
    end
  end
end

Contract:

{
  "consumer": {
    "name": "Test Message Consumer"
  },
  "provider": {
    "name": "Test Message Producer"
  },
  "messages": [
    {
      "description": "created;updated",
      "providerStates": [
        {
          "name": "Class1",
          "params": {
          }
        },
        {
          "name": "Class2",
          "params": {
          }
        }
      ],
      "contents": [
        {
          "first_name": "John",
          "last_name": "Doe"
        },
        {
          "hello": "world"
        }
      ],
      "matchingRules": {
      }
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "2.0.0"
    }
  }
}

See demo: AndrewJanuary/pact-ruby-demo#1

Comment on lines +39 to +41
message.events.map do |message_content|
Pact::Reification.from_term(message_content.contents)
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📓 It will return an array even for a single record, so when we have only one message content, we show an array [content] in Contract, when we have multiple messages, we show an array of arrays [[Content]]

@@ -13,7 +13,7 @@ def initialize message, decorator_options = {}
end

def as_json options = {}
hash = { :description => message.description }
hash = { :description => message.descriptions.join(';') }
hash[:providerStates] = provider_states
hash[:contents] = extract_contents
hash[:matchingRules] = extract_matching_rules
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 matchingRules is still only for a single message

@Mifrill Mifrill changed the title records sequence of messages feat([PoC]): Contract for sequence of messages. Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant