You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Synopsis: When trying to calling New-MockObject -Type 'Config' where Config is a class defined in another psm1 file, I get the following error PSInvalidCastException: 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
Client depends on Config
Client.Tests depends on Config & Client
Not sure if this is important, but Client has a using statement for Config, and therefore transitively, Client.Tests has 2 using calls to Config, 1 in Client.Tests and another in Client
using module .\Config.psm1
using module .\Client.psm1
Describe "SomeClassThatUsesAzureDevOpsAgentConfig" {
It "JoinAandB__A_and_B_Are_Populated__ReturnsAB" {
# Arrange
$mockConfig = New-MockObject -Type 'Config' -Properties {A = "foo" B = "Bar"}
$systemUnderTest = New-Object Client($mockConfig)
$expectedResult = "AB"
# Act
$actualResults = $objectUnderTest.JoinAandB()
# Assert
$actualResults | Should -Be $expectedResult
}
}
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
Starting discovery in 1 files.
Discovery found 1 tests in 16ms.
Running tests.
[-] SomeClassThatUsesAzureDevOpsAgentConfig.JoinAandB__A_and_B_Are_Populated__ReturnsAB 5ms (5ms|1ms)
PSInvalidCastException: Cannot convert the "Config" value of type "System.String" to type "System.Type".
ArgumentTransformationMetadataException: Cannot convert the "Config" value of type "System.String" to type "System.Type".
ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'Type'. Cannot convert the "Config" value of type "System.String" to type "System.Type".
at <ScriptBlock>, C:\Users\dgleason\source\repos\DevOpsTeam\IaC\installs\azure_devops_agent_configuration\Client.Tests.ps1:9
Tests completed in 77ms
Tests Passed: 0, Failed: 1, Skipped: 0, Inconclusive: 0, NotRun: 0
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 class
Steps 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
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: