Create a Pester script MyClass.GetID.Tests.ps1
containing something like this:
using module ".\MyClass.psm1"
Describe "GetID() method flows" {
InModuleScope "MyClass" {
$mockObject = [MyClass]::new()
$mockGuid = "guid"
Mock New-Guid {
return $mockGuid
}
Context "Happy flow" {
It "Should return the guid" {
$result = $mockObject.GetID()
$result | Should -Be $mockGuid
Assert-MockCalled New-Guid -Scope It -Exactly 1
}
}
}
}
You can also dot-source the class module:
. ".\MyClass.psm1"
Describe "GetID() method flows" {
BeforeAll {
$mockObject = [MyClass]::new()
$mockGuid = "guid"
Mock New-Guid {
return $mockGuid
}
}
Context "Happy flow" {
It "Should return the guid" {
$result = $mockObject.GetID()
$result | Should -Be $mockGuid
Assert-MockCalled New-Guid -Scope It -Exactly 1
}
}
}
Of course it is also possible to test all methods from the same test script.
Please note that PowerShell has no way to unload a class. This means changes to the class are usually not available until you restart the PowerShell session.