hello everyone :wave: I am getting more into pact ...
# pact-ruby
m
hello everyone 👋 I am getting more into pact these days and I might have a question for https://github.com/pact-foundation/pact-mock_service 🧵
I was trying a test script on the lines of:
Copy code
request_headers = { "Content-Type" => "application/x-www-form-urlencoded" }
request_body = URI.encode_www_form(
  [
    ...
    [
      "device_id",
      Pact.term(
        generate: "3387FE91-453B-4237-B986-17F4F0DC0E13",
        matcher: /^[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}$/
      )
    ]
    ...
  ]
)

interaction_mock_domain = "<http://127.0.0.1:9292>"
interaction_mock_path = "/interactions"
interaction_mock_headers = { "X-Pact-Mock-Service" => true }
interaction_mock_body = {
  request: {
    method: request_method,
    path: request_path,
    headers: request_headers,
    body: request_body
  },
  response: {
    body: response_body
  }
}.to_json
RestClient::Request.execute(
  url: interaction_mock_domain + interaction_mock_path,
  method: :post,
  headers: interaction_mock_headers,
  payload: interaction_mock_body
)
but it seems to me that I must be passing it wrong overall, because the term finally seems to not be matching (cause is maybe read as string):
Copy code
...
* Expected "Pact::Term matcher: /^[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}$/ generate: \"3387FE91-453B-4237-B986-17F4F0DC0E13\"" but got "3387FE91-453B-4237-B986-17F4F0DC0E13" at $.body.device_id[0]
...
-------------- I was able to put more thoughts and check more carefully and realised that I was likely doing still a bit of a mess .. 😅 reviewing where/what to encode and/or applying “.to_json” solved the issue, and this example worked for me 🙂
Copy code
request_body = {
  ..
  "device_id" => Pact.term(
    generate: "3387FE91-453B-4237-B986-17F4F0DC0E13",
    matcher: /^[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}$/
  ),
  ..
}
interaction_mock_body = {
  request: {
    method: request_method,
    path: request_path,
    headers: request_headers,
    body: request_body
  },
  response: {
    status: 200,
    headers: response_headers,
    body: response_body
  }
}.to_json
so I would say
t
you may have a better time with https://github.com/pact-foundation/pact-ruby
🙌 1
you would only need to invoke the mock service directly if you're doing something really custom
b
@Massimiliano De Vivo can you explain more about the underlying problem you’re trying to solve? As Tim says, it’s not normal to need to interact directly with the mock service.
m
the underlying problem that we wished to try to solve is to experiment if it is possible to replace a fully integrated client tests system with a set of tests that rely on pact to mock the network (but preserve the ui interactions part) we are aware of the recommendations to not use pact with UI
b
Yeah, personally, I’d use a general purpose mocking tool for that.
🙏 1
The pact mock was written for a specific purpose, and because of that, it doesn’t work very well for other usecases.
If you’re in Ruby world, I have used VCR quite happily in the past.
m
we also have some VCR setup, but we realised that maintenance of the recording + the dataset to be able to save the recording is rather heavy, hence we wanted to try new tooling which allows more re-usage
t
I agree with Beth that pact might not be the right tool for this
m
it might be true, but the current possibilities might still be better than other toolings, do you happen to have other recommendations in mind ? it would be very appreciated 🙏 🙂 we are evaluating different things
t
In what I understand of your system, I would lean a bit more into the pact model, and replace your integration tests with contract tests at the API level, and unit/module tests for the UI
By module test I mean what is sometimes called an “integration test”, but it doesn't leave your service boundary, ie you mock the API
I spoke a bit about this in the pact day last year, I can link you later when I am not on mobile

https://youtu.be/wkld_wRsTDE

Turned out to be easy to find. My section is around the 26:30 minute mark
m
you found it anyway 😄 , thank you for the info, we will peek into it 🙏
b