Comments (3)
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.
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.
Thank you @fflaten for this explanation!
from pester.
Related Issues (20)
- BeforeAll and AfterAll not invoked on skipped blocks when running test explicitly
- CodeCoverage.OutputEncoding ignored in xml encoding-attribute
- Invoke-Pester breaks on invalid CodeCoverage.OutputEncoding format
- TestResults.OutputEncoding option ignored
- Pester Should -Invoke -Scope "It" not working as expected HOT 1
- SkipRemainingOnFailure does not trigger on block failure
- Update how test registry entry is generated
- Improve Should-BeString assertion
- Fix Should-*Collection and their tests
- Make sure new assertion docs are in showing up on pester.dev
- Export Should-Invoke and Should-HaveParameter
- Output should result to Assert assertions
- Add soft assertions to new assertions
- Expose GlobalPluginData to all plugin steps
- Infinite loop for `FileInfo` and `DirectoryInfo` due to `Root` properties in `DirectoryInfo`. Should we restrict depth as a general fix in addition to specialcasing `DirectoryInfo` in `Get-DisplayProperty2`? HOT 1
- Measure Code Coverage for Pester
- `Get-Help Should-Be` will still show `Assert-Be`. We'll also need to modify `generate-command-reference.ps1` in docs-repo to rename files and title to match alias.
- `Get-EquivalencyOption` is not exposed, so unable to customize/no docs on expected hashtable. HOT 2
- Inconsistent behavior mocking external command between output verbosity 'Detailed' and 'Diagnostic' HOT 3
- Calling mock of Get-ChildItem with -File parameter fails on Linux
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pester.