-
-
Notifications
You must be signed in to change notification settings - Fork 467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mocking CimInstance does not work #1944
Comments
This does not seem to be related to the linked issue. New-Mock object simply creates uninitialized instance of the given type. There is probably a property getter that relies on internal state that the object does not have. Please show the full error. And ideally stacktrace. |
File contents
CommandInvoke-Pester -Configuration @{ Run = @{ Path = 'test.ps1' };Output = @{ Verbosity = 'Detailed' } }
Starting discovery in 1 files.
Discovering in C:\Users\foo\source\repos\rmd-devops-core\test.ps1.
Found 1 tests. 6ms
Discovery finished in 11ms.
Running tests from 'C:\Users\foo\source\repos\rmd-devops-core\test.ps1'
Describing Test
Describing foo
[-] bar 5ms (3ms|2ms)
at <ScriptBlock>, C:\Users\foo\source\repos\rmd-devops-core\test.ps1:7
NullReferenceException: Object reference not set to an instance of an object.
Tests completed in 105ms
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0 |
This is not Pester fault, internal state of the object is missing because you use intializer that does not set the internal state:
Namely it is CimPropertiesCollection.get_Item that fails. You can use one of the public constructors to get an instance of that type:
PS> $c = [Microsoft.Management.Infrastructure.CimInstance]::new("abc")
PS> Write-Host $c
abc |
The exception is caused by
You'd be better of creating a real ciminstance-object for the class (without saving it ofc) or you could use the CimInstance-constructor and generate what you need manually. # Real CIM-object as reference
$realProc = Get-CimInstance Win32_Process | Select -first 1
$realProc
ProcessId Name HandleCount WorkingSetSize VirtualSize
--------- ---- ----------- -------------- -----------
0 System Idle Process 0 8192 8192
$realProc.CimInstanceProperties.Count
45
# Mock
$procMock = [Microsoft.Management.Infrastructure.CimInstance]::new('Win32_Process','root/cimv2')
$procMock
ProcessId Name HandleCount WorkingSetSize VirtualSize
--------- ---- ----------- -------------- -----------
$procMock.CimInstanceProperties.Count
0
# No properties exists by default, only the extended aliasproperties from ETS in PowerShell
$procMock | Get-Member
TypeName: Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_Process
Name MemberType Definition
---- ---------- ----------
Handles AliasProperty Handles = Handlecount
ProcessName AliasProperty ProcessName = Name
VM AliasProperty VM = VirtualSize
WS AliasProperty WS = WorkingSetSize
#.... only base class stuff, none of the cim-properties you'd expect from Win32_Process
$name = [Microsoft.Management.Infrastructure.CimProperty]::Create('Name','MyProcess', [cimtype]::String, 'Property, ReadOnly')
$handlecount = [Microsoft.Management.Infrastructure.CimProperty]::Create('Handlecount', 123, [cimtype]::UInt32 , 'Property, ReadOnly')
$procMock.CimInstanceProperties.Add($name)
$procMock.CimInstanceProperties.Add($handlecount)
$procMock
ProcessId Name HandleCount WorkingSetSize VirtualSize
--------- ---- ----------- -------------- -----------
MyProcess 123
$procMock.CimInstanceProperties.Count
2 |
General summary of the issue
A mock object of 'Microsoft.Management.Infrastructure.CimInstance' does not work. When trying to use the created object the following error is thrown:
NullReferenceException: Object reference not set to an instance of an object.
Not sure if related, but see #345
Describe your environment
Steps to reproduce
Expected Behavior
The mocked object works.
Current Behavior
The mocked object does not work.
The text was updated successfully, but these errors were encountered: