liveBook · Manning

Should -Invoke

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

Should -Invoke is a test assertion command that verifies how many times a mocked command or function was called.

Should -Invoke verifies how many times a mocked command or function was called, making mock usage observable in unit tests. section 13.3.3

Use invocation counts to validate parameter-filter behavior:

  • Expect ConvertFrom-Html to be called once for the search. section 13.3.3
  • Expect Invoke-WebRequest to be called three times, once for each search result. section 13.3.3
  • Treat higher counts as evidence that a filter is too broad and lower counts as evidence that it is too restrictive. section 13.3.3

Validating a filtered mock

A test can mock ConvertFrom-Html with a URI parameter filter, run the search, and use Should -Invoke to confirm that only the intended call triggered the mock. section 13.3.3

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Counts invocations and interprets deviations as filter problems, while stopping short of validating real backend behavior.
Examples
Uses a KB search where ConvertFrom-Html should run once and Invoke-WebRequest should run three times.
What it assumes
Assumes familiarity with mocked commands, parameter filters, and unit-test setup.
What you can do after
Diagnose mock configuration by comparing observed invocation counts with expected command usage.
How it connects
Connects mocks, parameter filters, unit testing, integration testing, ConvertFrom-Html, and Invoke-WebRequest.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.