Code Monkey home page Code Monkey logo

Comments (4)

johlju avatar johlju commented on May 24, 2024 1

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.

nohwnd avatar nohwnd commented on May 24, 2024

@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.

ertanergunswapfiets avatar ertanergunswapfiets commented on May 24, 2024

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.

fflaten avatar fflaten commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.