Akshay Vashishtha
Akshay Vashishtha

Reputation: 68

change the response for single request among multiple request in nock

I have using express and trying to create an API for testing I used nock so i make a get request whose response I don't want to change after that i want to do put request and want to change the response of that part but when i tried to do that i getting error for the first get request that i don't want to change. I am getting the error Error: Nock: No match for request

  nock(baseURI + '/' + imp.http.relativeURI , { 'encodedQueryParams': true })
            .put('/' +imp.file.fileName)
            .reply(422,
              '<response><errormessage>some error message</errormessage></response>',
              [ 'Content-Type', 'application/xml; charset=UTF-8' ])

Upvotes: 1

Views: 1397

Answers (1)

Matt R. Wilson
Matt R. Wilson

Reputation: 7575

You need to use the allowUnmocked flag when mocking the second request. https://github.com/nock/nock#allow-unmocked-requests-on-a-mocked-hostname

nock(baseURI + '/' + imp.http.relativeURI , { allowUnmocked: true, 'encodedQueryParams': true })
        .put('/' +imp.file.fileName)
        .reply(422,
          '<response><errormessage>some error message</errormessage></response>',
          [ 'Content-Type', 'application/xml; charset=UTF-8' ])

This bit: nock(baseURI + '/' + imp.http.relativeURI) is basically telling Nock that you are testing this endpoint, and by default it assumes you only expect the calls explicitly mocked to that endpoint. allowUnmocked flips that assumption and allows live calls to proceed as normal unless you explicitly mock them out.

Upvotes: 1

Related Questions