-
-
Notifications
You must be signed in to change notification settings - Fork 477
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
How to test ScriptBlock passed to mocked function #1042
Comments
You are running still in the same scope so you can mock the git command and run the script as you normally would and then assert that git was called with the correct params. Like this: Describe "-" {
it "-" {
# -- Arrange
# at the moment we are not adding alias without .exe to executables
# so your best bet is to create a function with name 'git' and mock that
# other options is mock git.exe and call git.exe everywhere, but that is not
# cross-platform
function git () { }
Mock git { }
# -- Act
# this is in your code
& { git clone --single-branch --progress -b 1.0.0 "http://domain.com/abc.git" "here" }
# -- Assert
# you can use -ParameterFilter { $true ; (Write-Host $args) } to see the incoming value in console
Assert-MockCalled git -ParameterFilter {
# make sure you put the string on the left side, $args is an array so it would not equal otherwise
"clone --single-branch --progress -b 1.0.0 http://domain.com/abc.git here" -eq $args }
}
} |
I change the test as follows: Describe 'Git clone' {
Function git {}
Mock git { Write-Host $args }
It 'Uses token when in same VSTS server' {
Mock Test-SameTfsServer { return $true }
Mock Get-EnvironmentVariable { return 'token' } -ParameterFilter { $Name -eq 'SYSTEM_ACCESSTOKEN' }
Invoke-GitCloneRepository -Uri 'http://dummy/repo.git' -BranchTag 'master' -Path TestDrive:\repo.git
Assert-MockCalled git -ParameterFilter { $args -icontains '-c' } # test passes
Assert-MockCalled git -ParameterFilter { $args -icontains '-c http.extraheader' } # test fails
}
} and here's the result:
FYI, I don't change the implementation at all. It looks like |
Finally I can get it works using your example. I'm new to PowerShell, I don't understand why when Describe 'Git clone' {
Function git () {}
Mock git { }
Mock Write-Host { }
It 'Uses token when in same VSTS server' {
Mock Test-SameTfsServer { return $true }
Mock Get-EnvironmentVariable { return 'token' } -ParameterFilter { $Name -eq 'SYSTEM_ACCESSTOKEN' }
Invoke-GitCloneRepository -Uri 'http://dummy/repo.git' -BranchTag 'master' -Path TestDrive:\repo.git
Assert-MockCalled git -ParameterFilter { '-c http.extraheader=Authorization: bearer token clone --single-branch --progress -b master http://dummy/repo.git TestDrive:\repo.git' -eq $args } # passes
Assert-MockCalled git -ParameterFilter { $args -eq '-c http.extraheader=Authorization: bearer token clone --single-branch --progress -b master http://dummy/repo.git TestDrive:\repo.git' } # failed
} |
My question has answer. I'll close this issue. |
I'm new to Pester, and I'd like to test my git cloner script. I create
Invoke-VerboseCommand
to ensure external command executed, either it throw error or not. Of course, it should show the executed command.I'm using Windows 10, Pester 4.3.1, PowerShell 5.1
This is script that I'd like to test:
and here's my test
The goal is testing script block passed to
Invoke-VerboseCommand
function. How can I achieve this?The text was updated successfully, but these errors were encountered: