Open dglambert opened 1 week ago
Closing the loop on this, I was able to make this work by changing -Type 'Config'
to -Type ([Config])
- credit for the answer goes to @mklement0 at https://stackoverflow.com/questions/78979730/how-to-use-pester-new-mockobject-with-class-from-within-a-module
However, I'm not sure why this works, I think the documentation at the very least should be updated.
Glad you figured it out. Likely same explanation as this.
Let's leave this open to update matching docs as you've suggested. 🙂
If you or someone would like to contribute a PR, I suggest adding a note about using parantheses around type-values in -Type
parameter help + a fixed example in https://github.com/pester/Pester/blob/main/src/functions/New-MockObject.ps1.
Checklist
What is the issue?
Synopsis: When trying to calling
New-MockObject -Type 'Config'
where Config is a class defined in another psm1 file, I get the following errorPSInvalidCastException: Cannot convert the "Config" value of type "System.String" to type "System.Type"
Using Powershell 5.1 with Pester V5.6.1
I have put together the following simple sample to demonstrate the problem
All files are in the same directory
Config.psm1
Client.psm1
Client.Tests.psm1
I'm not including the JSON since its not relevant, as I am trying to mock the config class, so it should not be needed.
The Error message I get when I run
Invoke-Pester .\Client.Tests.ps1
The weird thing is, when I was developing my unit tests, they were all passing, it wasn't until some point till I made it to my 6th or 7th test that it started failing. This makes me think the way I had my module loaded in my IDE (VS Code) bypassed the issue.
Expected Behavior
Calling
New-MockObject -Type 'Config'
should return my mocked classSteps To Reproduce
Executing
Invoke-Pester .\Client.Tests.ps1
from current working directory in IDE terminal after closing and reopenning, or via new Powershell window to avoid any caching of classes/modules.Describe your environment
Pester version : 5.6.1 C:\Program Files\WindowsPowerShell\Modules\Pester\5.6.1\Pester.psm1 PowerShell version : 5.1.19041.4648 OS version : Microsoft Windows NT 10.0.19045.0
Possible Solution?
No response