-
-
Notifications
You must be signed in to change notification settings - Fork 470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should -BeOfType should support custom types #1315
Comments
@KirkMunro Thanks for the suggestion, does |
You can see a test where I am checking for type The I also considered submitting this issue as a request for a |
Yes that should work, the assertion is using
Might be worth checking what the PS team thinks about integrating the behavior into I think that |
It would be great to see a clean solution for this. I'm currently using Pester 5.1.1 and the following workaround:
|
Either PowerShell/PowerShell#10750 is resolved or, as a workaround, if the first -is match fails, try the GetType().Name method. Definitely also log that it was matching for a custom type. If implemented this way it would still work in downstream testing (e.g. 5.1+) @nohwnd this is something I think I could figure out a PR for if you're interested. |
The PowerShell PR is stuck and won't reach all users of Pester, so I think we should support this. I would suggest a switch like Automatic fallback will make it harder to deprecate later if moved to it's own function or parameter set - or replaced by native support. |
I think I changed my mind. Making it consider both real type, and PSType by default does not seem to be such a bad thing. When user sets the type, they are unlikely to set it to something random or stupid (like System.Object), but rather purposefully set it to either:
The room for error is there, but I'd rather learn how to do it right when it bites me maybe once, than always have to remember specifying UsePSTypeNames. Second best option is imho having a separate parameter for it:
|
1. General summary of the issue
PowerShell supports custom object types via the
PSTypeNames
parameter onPSObject
instances. Pester has a-BeOfType
parameter on theShould
command to allow testers to verify if an object is of a given type. At the time of this writing,Should -BeOfType
only supports checking against actual .NET types. It would be very helpful if Pester also supported custom type names here, such that the internal logic first looked at the actual type, and if the actual type was of typePSObject
andPSObject
was not a match for the type being looked for, then looked at thePSTypeNames
collection to see if the required type name was contained within that collection.The text was updated successfully, but these errors were encountered: