Mock

The ability to mock commands is a prominent feature of Pester. Mocking is used to reduce the scope of a set of tests.

Creating a Mock overrides a command by taking a partial copy. The copy includes the param and dynamicparam blocks, but excludes any command implementations.

Mocks can be created under the Describe or Context keywords.

Commands are mocked by using the Mock keyword:

Describe Subject {
Mock Get-Date
}

If a command returns a value, a body can be defined for the Mock to simulate the normal operation of the command. In the following example, the string 01/01/2017 is returned in place of a normal response from Get-Date:

Describe Subject {
Mock Get-Date { [DateTime]::new(2017, 1, 1) }
}

In the preceding example, the script block is a positional argument for the MockWith parameter. The mock might also be written as follows:

Describe Subject {
Mock Get-Date -MockWith {
[DateTime]::new(2017, 1, 1)
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.153.160