Thanks for taking the time to read my question!
I'm having an issue \w Pester. When several tests are run with one Invoke-Pester cmd, I suddenly get PSInvalidCastException on blocks where I'm passing an inherited test class to a method that takes its parent, for example:
class TestInterceptor : HeaderInterceptor
{
[void] Intercept([ApiRequest]$Request)
{
.... # override \w test data
}
}
[void] AddClientHeaderInterceptor([HeaderInterceptor]$HeaderInterceptor)
{
....
}
It 'Adds new host interceptor to all clients' {
$interceptor = [TestInterceptor]::new()
FAILS>> $testService.AddClientHeaderInterceptor($interceptor)
}
ERROR:
Cannot convert argument "HeaderInterceptor", with value: "HeaderInterceptor", for "AddClientHeaderInterceptor" to type "HeaderInterceptor": "Cannot convert the "HeaderInterceptor" value of type "HeaderInterceptor" to type "HeaderInterceptor"
The tests pass when run on their own (Invoke-Pester $testFile) in the same PWSH session, but when run with Invoke-Pester -configuration $config (Default config) and ran against all test files it fails. I've thus far been unable to replicate by running each test manually, in that case everything passes.
The only change is that I've added a using module to a module that is widely imported.
I can take that same test class and manually cast it in a separate pwsh session.
using module ApiClientCore
class TestInterceptor : HeaderInterceptor
>> {
>> [void] Intercept([ApiRequest]$Request)
>> {
>> $Request.Headers['Foo'] = 'Bar'
>> }
>> }
>$t = [TestInterceptor]::new()
>[HeaderInterceptor]$t
TestInterceptor
Through debugging, I have a vague sense that it has something to do \w the AppDomain loading in different version of the same object and when attempting to cast between versions I get the error above. That piece is outside of my understanding atm however.
>$t = [TestInterceptor]::New()
>$t.GetType().Assembly.FullName
PowerShell Class Assembly, Version=1.0.0.69, Culture=neutral, PublicKeyToken=null
>[HeaderInterceptor]::new().getType().Assembly.FullName
PowerShell Class Assembly, Version=1.0.0.71, Culture=neutral, PublicKeyToken=null
>[HeaderInterceptor]$t
InvalidArgument: Cannot convert the "TestInterceptor" value of type "TestInterceptor" to type "HeaderInterceptor".