Description
Checklist
- Issue has a meaningful title
- I have searched the existing issues. See all issues
- I have tested using the latest version of Pester. See Installation and update guide.
What is the issue?
I want to create a mocked class by inheriting an existing class B. The purpose is to mock one of its methods.
However, when I do this, I get Unable to find type [B].
I am using a standard way of structuring my module where class files exist in a classes folder and are dot sourced in the .psm1 file.
Example of this structure:
https://github.com/saidbrandon/PureStorage.FlashArray.Reporting/tree/master/PureStorage.FlashArray.Reporting
Expected Behavior
Pester should not give the error "Unable to find type [B]"
Steps To Reproduce
Unit test file:
using module ".\MyModule.psd1"
BeforeAll {
Import-Module $PSScriptRoot\MyModule.psd1 -Force
}
Describe 'My unit test 1' {
It 'Runs unit test 1' {
# Mock a private function
# Gives: Unable to find type [B].
class MockedClassB : B
{
MockedClassB ([int]$foo) : base($foo)
{
}
[string] MyMethod() { return 'MockedString' }
}
$mockedB = [MockedClassB]::new(13)
$mockedB.Run()
}
}
Describe 'My unit test 2' {
It 'Runs unit test 2' {
# No errors.
$instanceB = [B]::new(13)
$instanceB.Run()
}
}
MyModule.psd1:
@{
RootModule = 'Include.psm1'
ModuleVersion = '1.0.0'
# This is needed to use classes. Otherwise the Test file will give "Unable to find type"
ScriptsToProcess = @(
'Classes\B.ps1'
)
RequiredModules = ''
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @()
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
# Variables to export from this module
# VariablesToExport = '*'
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()
}
Include.psm1:
#Requires -Version 5.0
[cmdletbinding()]
param()
Write-Verbose $PSScriptRoot
Write-Verbose 'Import Classes in order because of dependencies'
$classList = @(
'B'
)
foreach($class in $classList)
{
Write-Verbose " Class: $class"
. "$psscriptroot\Classes\$class.ps1"
}
B.ps1:
using module ".\..\..\ModuleContainingClassA\ModuleContainingClassA.psd1"
class B: A
{
# ctor
B([int]$foo)
{
}
}
Describe your environment
Pester version : 5.3.3
PowerShell version : 5.1.19041.1682
OS version : Microsoft Windows NT 10.0.19044.0
Possible Solution?
The workaround for now is to remove the dot sourcing in the psm1 file and instead have all the classes inside the psm1 file itself.