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

Expecting calls to the same path in the same test #23

Open
ybce opened this issue Jan 15, 2020 · 2 comments
Open

Expecting calls to the same path in the same test #23

ybce opened this issue Jan 15, 2020 · 2 comments

Comments

@ybce
Copy link

ybce commented Jan 15, 2020

I have a scenario where I am setting up expectations to call an external service twice (mocking that in my tests using mock-https-server), one which returns an empty array and one that returns a response.

something like this:

  expectGetResourceByParamX (queryParams, statusCode, body) {
    this.server.on({
      method: 'GET',
      path: '/v1/resource',
      filter: (req) => {
       // filter code
        return true
      },
      reply: {
        status: statusCode,
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify(body)
      }
    })
  }

  expectGetResourceByParamY (queryParams, statusCode, body) {
    this.server.on({
      method: 'GET',
      path: '/v1/resource',
      filter: (req) => {
       // filter code
        return true
      },
      reply: {
        status: statusCode,
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify(body)
      }
    })
  }

The only difference is what query params I use on that same exact path, however when setting up the test this seems to cause an issue since the handlers have the same path and the requests that come in are processed by the wrong handler. I looked at the library code but can't see an obvious place where this can be fixed.

Any ideas on how this sort of problem can be solved (if it can be) using this library or is this some sort of limitation of the library.

Thank You.

@ybce
Copy link
Author

ybce commented Jan 15, 2020

I just looked at the pull requests in the repo and this #1 might be something that can help with my issue.

@pracucci
Copy link
Collaborator

I'm not sure I'm getting your use case. In the example provided, the two functions expectGetResourceByParamX() and expectGetResourceByParamY() are identical, but I would expect in the real world to differ. Depending on how they differ, you may end up with a different solution.

Generally speaking, I would suggest you to look at the fact that the body can also be specified as body: function(req) {}: this way you can dinamically generate the response based on the request, but you can also keep track of each received request for later inspection / assertion on it.

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

No branches or pull requests

2 participants