Comments (4)
you could add some fake functions inside BeforeAll
@ertanergunswapfiets If you need stub function for Invoke-SqlCmd
you can use the stub module ones here (to run unit tests with): https://github.com/dsccommunity/SqlServerDsc/tree/main/tests/Unit/Stubs
Or, create your own using the steps in the README.
from pester.
@ertanergunswapfiets moved your issue here, from the closed issue. Can you plese post how you define the mock? And are you trying to mock in some module scope?
from pester.
First of all the script I am trying to test is running as a Runbook in Azure so it is not a function. I thought it was the problem and I put it it in a function to test it. here is the script file i am trying to test now script.ps1:
function Runbook {
param (
[Parameter (Mandatory= $true)]
[string]$TargetDatabaseName,
[Parameter (Mandatory= $true)]
[string]$TargetServer
)
Import-Module Az.Sql
$script = @"the script"
$sqlCredential = Get-AutomationPSCredential -Name "credentials"
$params = @{
'ServerInstance' = $TargetServer;
'Database' = $TargetDatabaseName;
'Username' = $sqlCredential.UserName;
'Password' = $sqlCredential.GetNetworkCredential().Password;
'Query' = $script
}
Invoke-Sqlcmd @params
}
Originally the runbook funtion didnt exist so I just surrounded the actual script with a funtion.
Here is the test file script.Tests.ps1:
BeforeAll {
$scriptFile = $PSCommandPath.Replace('.Tests.ps1', '.ps1')
. $scriptFile
}
Describe 'Test Script' {
BeforeAll {
Mock Get-AutomationPSCredential -ParameterFilter { $Name -eq 'credentials' } -MockWith {
$username = 'testUser'
$password = ConvertTo-SecureString 'MySecretPassword' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $password)
return $credential
}
Mock Invoke-Sqlcmd -MockWith { result=1 }
It 'It should not return error' {
Runbook "test" "test" | Should -Be 1
}
}
I also tried Mock command with giving ModuleName CommandName without MockWith but i couldn't succeed.
This is the output of test:
> Invoke-Pester ./runbooks/AnonymiseDB.Tests.ps1 -Output Diagnostic
Pester v5.5.0
Discovery: Starting test discovery in 1 test containers.
Starting discovery in 1 files.
Discovery: Discovering tests in /deducted/runbooks/script.Tests.ps1
Discovery: Found 1 tests in 12 ms
Discovery: Processing discovery result objects, to set root, parents, filters etc.
Discovery found 1 tests in 23ms.
Discovery: Test discovery finished.
Running tests.
Running tests from '/deducted/runbooks/script.Tests.ps1'
Mock: Setting up parametrized mock for Get-AutomationPSCredential.
Mock: We are in a block, one time setup or similar. Returning mock table from test block.
Mock: Resolving command Get-AutomationPSCredential.
Mock: Searching for command Get-AutomationPSCredential in the script scope.
Mock: Did not find command Get-AutomationPSCredential in the script scope.
Describing Test Script
[-] Describe Test Script failed
CommandNotFoundException: Could not find Command Get-AutomationPSCredential
Tests completed in 163ms
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0
BeforeAll \ AfterAll failed: 1
- Test Script
I have 2 functions/commands to mock, one is Get-AutomationPSCredential and other is Invoke-Sqlcmd. When I comment out one I confirm that the other also fails so issue is with mocking both.
from pester.
The original commands has to be available in your session for Pester to be able mock them, as it needs to see which parameters to copy etc. Usually you'd import the necessary modules/dependencies before invoking the tests.
If you don't have access to the modules containing Get-AutomationPSCredential
and Invoke-Sqlcmd
, you could add some fake functions inside BeforeAll
so they exist prior to calling the Mock
-command.
from pester.
Related Issues (20)
- Issues calling a Mock when a Mandatory attribute is included on a parameter HOT 3
- Should -HaveParameter -DefaultValue does not work for boolean types or integer value of 0 (regression) HOT 2
- Pester module warning 'CN=Jakub Jareš, O=Jakub Jareš, L=Praha, C=CZ' HOT 15
- Pester CI Pipeline Should Halt on 'BeforeDiscovery' Failure HOT 3
- Failure in Discovery when using different parameters across files HOT 3
- Select-Object and New-MockObject - Discarded PSObject HOT 2
- Bring back 'InconclusiveCount' property (or alternative) on returned Pester.Run object HOT 19
- I’m not a developer I run a nonprofit and a Pester Power Shell is running HOT 2
- Pester stopped working in vscode HOT 2
- v5.5 $____Pester.CurrentTest not populated in AfterEach HOT 4
- Missing timestamp from test suite attributes in junit xml export HOT 1
- Unbound container scriptblock unexpectedly runs in Pester scope HOT 10
- Pester 5.5 reports incorrect Skipped results for Pending and Inconclusive HOT 3
- BeOfType doesn't see types, loaded from a file with classes, but the "It" function - does HOT 3
- Refer to a script in a parent directory HOT 2
- Does somthing like @afterstep exist for Pester 4.6 with Invoke Gherkin HOT 1
- New-RandomTempRegistry: Test-Path : No more data is available HOT 2
- Provide a way to more easily modify a mocked parameter before comparing with a test value. HOT 3
- Error during handling of output-element within Write-NUnit3OutputElement HOT 1
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.