Code Monkey home page Code Monkey logo

Comments (3)

fflaten avatar fflaten commented on May 25, 2024 2

Thanks for the report.

TLDR: Using [MyTestClass] as a parameter value or argument passes the value as a string. The type isn't resolvable by name internally in modules like Pester (see long version for why). If you wrap it in parentheses you'll pass the type-object itself which should work.

Try $TestO | Should -BeOfType ([MyTestClass]) or $TestO.GetType().Name | Should -Be 'MyTestClass'


Long version 🙂

By dot-sourcing Classes.ps1 you only define your classes locally in the current scope. Functions like Should run in a different module-specific session state and scopes which can't see the class in your script scope. The issue is similar to running the script in your session without dot-sourcing.

# Run in child scope similar to your test file. Class is not available when the script is finished -> child scope with class imported is deleted
> & ./Classes.ps1; [MyTestClass]      
InvalidOperation: Unable to find type [MyTestClass].

The typical solution would be to place the class in a module like Classes.psm1 and use e.g. using module .\Classes.psm1 at the top of your test-file to import it. That should've imported it in the global session state, making it available to every module and script. Unfortunately it doesn't. Maybe PowerShell imports the classes in the current scope in the global state which results in the same behavior.

Unfortunately working with classes in PowerShell has lots of weird behavior like this.

A few workarounds:

  • Wrap the type in parentheses to pass the actual type-object
  • Import your classes before running Pester using either using module .\Classes.psm1 or dot-source . .\Classes.ps1. That should make them available in all scopes and session states, including Pester's internal lookup that currently fails.
  • Use $TestO.GetType().Name | Should -Be 'MyTestClass' to assert the type name in this particular scenario.

from pester.

fflaten avatar fflaten commented on May 25, 2024

Keep open until we've updated docs and function help with an example that passes the type-object properly (with parentheses).

Maybe also be clean up own internal tests, besides https://github.com/pester/Pester/blob/main/tst/functions/assertions/BeOfType.Tests.ps1 which is already good.

from pester.

exchange12rocks avatar exchange12rocks commented on May 25, 2024

Thank you @fflaten for this explanation!

from pester.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.