Comments (5)
Hi. This should work. Have you verified the contents of $azResources
? And/or the value of $LASTEXITCODE
before these lines?
$LASTEXITCODE
is only set by applications, so the value 1
could originate from a prior command. If the mock works as expected, it wouldn't change that variable back to 0
.
from pester.
Hi @fflaten, I know the return value from the az command will be under JSON format so my actual mock looks like this:
Mock az { '[ { "id": "something...", "name": "something..." } ]' } -ParameterFilter {"$args" -match 'resource list'}
then I passed arguments for the script that I want to test:
& "$PSScriptRoot\myscript.ps1" $arg1 $arg2...
Should -Invoke az
I just checked the $LASTEXITCODE
(by using Write-Host $LASTEXITCODE
) before and after executing az
command the value of $LASTEXITCODE
is blank. I commented out all the code in that script and leave only 2 lines as given, it throws the same error.
Update: I checked the content of $azResources
. It matches with the value I set in my mock.
from pester.
Update: I checked the content of
$azResources
. It matches with the value I set in my mock.
Then $LASTEXITCODE
is changed by something else in the script if it changes during the test. Pester only modifies it at the end of Invoke-Pester
to number of failed tests.
Isolated repro:
Describe 'd' {
It 'i' {
& whoami # to reset lastexitcode
$LASTEXITCODE | Should -Be 0 -Because 'should not be non-zero before we start'
$scriptContent = @'
$azResources = (az resource list --resource-group $args[0]) | ConvertFrom-Json
if ($LASTEXITCODE -ne 0) { throw "az resource list returned a $LASTEXITCODE exit code." }
$azResources
'@
Set-Content -Path 'TestDrive:/myscript.ps1' -Value $scriptContent
Mock -CommandName az -ParameterFilter { "$args" -match 'resource list' } -MockWith {
'[ { "id": "something...", "name": "something..." } ]'
}
$res = & 'TestDrive:/myscript.ps1' 'hello'
$res.id | Should -Be 'something...'
$LASTEXITCODE | Should -Be 0
}
}
# Tests pass
So ready to close? 🙂
from pester.
Hi @fflaten. Thanks for your suggestion. I followed your code above and run from my side. Everything works fine. However, when I tried to do something like this:
myscript.ps1:
write-host $LASTEXITCODE # gives me blank here leading to if statement
$azResources = (az resource list --resource-group $args[0]) | ConvertFrom-Json
if ($LASTEXITCODE -ne 0) { throw "az resource list returned a $LASTEXITCODE exit code." }
myscript.Tests.ps1
Describe 'a' {
It 'testing' {
Mock -CommandName az -ParameterFilter { "$args" -match 'resource list' } -MockWith {
'[ { "id": "something...", "name": "something..." } ]'
}
$res = & './myscript' 'hello'
}
}
For some reasons, the $LASTEXITCODE
starts from blank instead of 0 as you said. I have no idea why this happens. I don't know if I have to reset like the way you did before executing the script to set $LASTEXITCODE to 0
from pester.
I don't know and can't reproduce unfortunately. Code above prints 0
for me as expected.
You could try Write-Host $global:LASTEXITCODE
and see if that works, in case something has set $LASTEXITCODE
to ex. $null
earlier in your code (or session profile?). Whatever it is, it doesn't seem to be Pester-specific, so closing this issue. 🙂
To further troubleshoot, I can recommend the PowerShell Discord-server (https://aka.ms/psdiscord). There's also a #testing
channel for discussion regarding Pester and other testing.
from pester.
Related Issues (20)
- Pester fails to import modules or files even with full path HOT 11
- Not reloading PowerShell class before new test run HOT 3
- ANSI sequence included in Azure Pipelines error result
- Testing of ForEach-Object -Parallel HOT 3
- Pester container data dictionary object gets populated with test parameters after invoke-pester HOT 3
- Default values for parameters in a different set are added to Data HOT 1
- Changes to undefined script parameters during BeforeDiscovery are added to Data
- Use Pester to mock kubectl command
- pester.dev site does not load properly HOT 2
- Mocking a function with OrderedDictionary parameter throws error with PowerShell 7, runs fine with PowerShell 5 HOT 1
- Should -ActualValue $result.TraceFlag -BeOfType 'System.UInt32[]' regression.
- `$SecureString |Should -be` testable HOT 3
- Errors from tests not displaying variables HOT 1
- ScriptBlocks as advanced alternative to name templating for data-driven tests HOT 4
- Test execution sometimes throws a NullReferenceException error HOT 7
- Pester throws an exception when run inside runspace HOT 13
- Missing tests for custom assertions
- Mocks of functions in nested modules fail on Invoke-Pester wildcard discovery HOT 3
- Issues calling a Mock when a Mandatory attribute is included on a parameter HOT 3
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.