Mocking Rest API endpoint

Welcome Forums Pester Mocking Rest API endpoint

This topic contains 2 replies, has 3 voices, and was last updated by

4 months, 2 weeks ago.

  • Author
  • #114532

    Topics: 1
    Replies: 1
    Points: 12
    Rank: Member

    Does anyone have a recommendation on mocking a Rest API endpoint with Pester? I have a number of modules that use invoke-restmethod to consume Rest APIs and am able to unit test a function with something like this:

    Mock -CommandName Invoke-RestMethod -Parameters { $PSBoundParameters.body -eq '{"submit1": "A Value"}'} -MockWith { return (convertfrom-json '{"return1": "Some value"}') }

    This works well for making sure a function sends the correct data to Invoke-RestMethod and processes the expected results, but it depends upon knowing how the function was implemented internally; i.e. the function can't be changes to use Invoke-WebRequest without changing all the Mocks and tests.

    I am thinking that if I was able to mock the web response, rather than the Invoke-RestMethod or Invoke-WebRequest, unit testing would be more effective, but I haven't been able to figure out a good way to the that with Pester.

  • #114553

    Topics: 8
    Replies: 809
    Points: 2,385
    Helping Hand
    Rank: Community Hero

    I would say whatever you have now is the best possible, mocks are for mocking cmdlet/functions and you are in a way mocking the web response.

  • #133083

    Topics: 0
    Replies: 3
    Points: 126
    Rank: Participant

    I may try to create a helper function to handle invoke-restmethod across your module. This mean less code to handle error processing, you can have your own logic to process queries and it's easier to mock.

The topic ‘Mocking Rest API endpoint’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort