Pester: Unable to find type

Tagged: 

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 2 weeks, 3 days ago.

  • Author
    Posts
  • #58255
    Profile photo of Greg Tate
    Greg Tate
    Participant

    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?

    Thanks!

  • #58372
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

    • #58375
      Profile photo of Greg Tate
      Greg Tate
      Participant

      Thanks Don. That makes sense and gives me some direction to go from here. Appreciate the reply!

  • #58379
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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?

You must be logged in to reply to this topic.