-
-
Notifications
You must be signed in to change notification settings - Fork 468
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
Unable to Mock New-Object #311
Comments
Haven't been able to reproduce this yet. I can't see your calling code in the ExamineContentZip module, but this attempt to reproduce the problem worked fine for me: Describe 'Mocking Test' {
$zipMock = New-Module -AsCustomObject -ScriptBlock {
function Namespace {
New-Module -AsCustomObject -ScriptBlock {
function Items {
return @{Name = 'TextFile.txt'}
}
}
}
Export-ModuleMember -Variable * -Function *
}
Mock New-Object { return $zipMock } -ParameterFilter { $ComObject -eq 'Shell.Application' }
It 'Calls the mock' {
New-Object -ComObject Shell.Application | Should Be $zipMock
}
} |
Module basically contains this:
Test is this:
Right now when I remove the ModuleName flag I do not see the New-Object call mocked at all.
Result:
When I leave the ModuleName in it basically returns a null value.
|
Ah, you're running into issue #204 . It's sort of a bug, but it's one of those things I put off fixing for a major release since it might break people's existing scripts to fix it. In the meantime, using the Remove-Module [e]xamineContentZip -Force
$filePath = Join-Path -Path $PSScriptRoot -Child 'ExamineContentZip.psm1'
Import-Module -Name $filePath -DisableNameChecking -ErrorAction Stop
InModuleScope ExamineContentZip {
Describe 'functiontotest' {
Context 'Should return null if TextFile is present' {
$zipMock = New-Module -AsCustomObject -ScriptBlock {
function NameSpace {
New-Module -AsCustomObject -ScriptBlock {
function Items {
return @{Name = 'TextFile.txt'}
}
}
}
Export-ModuleMember -Variable * -Function *
}
Mock New-Object { return $zipMock } -ParameterFilter { $ComObject -eq 'Shell.Application' }
$expected = $null;
$returned = functiontotest -ZipFile 'Test'
It 'Should return null' {
$returned | should be $expected
}
}
}
} Alternatively, don't assign your object to the $zipMock variable. Just put all of that New-Module code right into the -MockWith script block, and it would work that way as well. The problem is that the -MockWith block was being executed inside the module, and the $zipMock variable was out in your test script where the module couldn't see it. |
Crap I was afraid that's what I was running into. I tried both your suggestions and it started working. Could we highlight this in the documentation as I spent way to much time on this this morning. Totally get waiting for a major release to fix if its going to break things. |
I think it's about time to just push for the next major release; we have several of these nagging things that are waiting to be fixed. |
Seems like a really good idea to me. This one in particular is kind of a painful bug to run into as its kind of difficult to track down exactly where the problem is. |
In my code I'm trying to mock a call to New-Object. I'm wondering if this is similar to #296
The relevant chunk of the code is as follows:
All attempts to mock this code have been unsuccessful. Most of the time it acts as if I haven't actually called new object which results in $shell being null.
I've attempted to mock the functioning as such:
This was done over three separate attempts each attempting a different mock or the finally the function. What's strange is that if I break into the function before calling $shell I can manually paste in the Mock code and it'll work. This only appears to fail if I'm calling it from the test.
The text was updated successfully, but these errors were encountered: