Just getting started with Pester and want to test that my function returns an object of custom type. I am using the test {Get-CustomObjectType} | Should Be ([CustomObjectType]). However, when running the test I get an error message RuntimeException: Unable to find type [CustomObjectType].
In the test file, ISESteroids shows a squiggly line indicating ‘CustomObjectType’ is unknown. You may have to load an assembly first. You may have to run this script once to load non-default assemblies. Adornments will be refreshed once you change your script code.
I dot-sourced my test script file and even confirmed that CustomObjectType exists via the Get-TypeData command.
Any ideas or suggestions on how to get around this issue?
Well… it’s not so much an “issue,” I guess, as the way PowerShell works. [CustomObjectType] isn’t known to the shell, and assuming that all you’re doing is applying a custom type name to a PSObject, then there’s not really a stellar workaround. Because your type isn’t “in” .NET, then it doesn’t exist in the shell.
And here’s the thing - according to OOP principles, this shouldn’t work. A type is meant to be a contract - a fixed interface. If you’re just applying a name to something, then you don’t have a declarative, actual type. Now, if you’re doing this with classes, that’s different.
One thing that comes to mind would be writing a test to check for the output object having the desired members - that way you could validate the structure that you expect?