Hi all I have written a script to display the current user info, I would like to write Pester test case that should mock the output, also if I don’t have return in the function how can I write a test for that too
function Get-CurrentUserInfo
{
$domain = [Environment]::UserDomainName
$user = [Environment]::UserName
if (!([string]::IsNullOrEmpty($domain))) { $domain = $domain + '\' }
$currentUser = $domain + $user
#return $currentUser I have commented out so that it will not return any output
}
Here is my test case when there is return
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Get-CurrentUserInfo.ps1"
Describe "CurrentUser" {
It "CurrentUser Info" {
Get-CurrentUserInfo | Should be 'MY-PC\username'
}
}
Which works fine with my PC but when I execute the same in other PC it will fail so how can I make it unique
I would change the Should be ‘MY-PC\username’ to Should be “$($env:ComputerName)$($env:UserName)” in your Pester test. That should get you around mocking the function in the first place.
For the example you’ve provided you don’t really need to use mocking because you can just test for the correct value. If you mock a function you’re not testing it because the mock function will be invoked instead.
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Get-CurrentUserInfo.ps1"
Describe "CurrentUser" {
It "CurrentUser Info" {
Get-CurrentUserInfo | Should be "$($env:UserDomainName)\$($env:UserName)"
}
}
Example 2 (Get-CurrentUserInfo replaced by Mock function):
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Get-CurrentUserInfo.ps1"
Describe "CurrentUser" {
It "CurrentUser Info" {
Mock Get-CurrentUserInfo -MockWith { return 'Hello PowerShell' }
Get-CurrentUserInfo | Should be 'Hello PowerShell'
}
}
Your “Should Be” test doesn’t need to reflect the actual output of the function because you can’t just fake it because your original function is never being invoked.
I hope above makes sense. If not, please let us know.
Hi Daniel thanks for the code, but I am not able to debug my main function to see whats actually happening. If I save the file as .psm1 instead of .ps1 will the logic remains same or will it change
That thing I got but coming to mocking will it be same?
Import-Module "D:\MyFile.psm1" -Force
Describe "My Test" {
It "My Test" {
Mock My-Function -MockWith { return 'Hello' }
My-Function | Should be 'Hello'
}
}
Well no value returned should basically be a NULL so I would assume the following:
Describe "My Test" {
It "My Test" {
Mock My-Function -MockWith { $null }
My-Function | Should be $null
}
}
Depending on how your module is written though you may need to look in to using inmodulescope InModuleScope · pester/Pester Wiki · GitHub but for the most part mocking with a simple function being imported like that will work the same.
Krishna, that is correct. If you’ve mocked the function you’re trying to test it will never be called.
One wouldn’t mock the function to be tested but any function being called by the function under test because you can’t provide the correct environment or access to an external resource like a database or API.
OK I have a script which creates a registry if it is 32 bit it will return respective path if not it will 64 bit path. I mocked that one and when I execute it I am not able to see the registry getting created.
If you need to write something to a file for a test then you don’t really need to mock it. You just do it. You generally use mock to return data you know will test your logic.
Function Test-writeHost
{
Add-Content 'C:\TEMP\duplicates.txt' 'The Message'
}
Describe "Test write" {
It "Test write" {
Test-writeHost
Get-Content -Path 'C:\TEMP\duplicates.txt' | Should be 'The Message'
}
}
For instance you’d mock the get-content command to return a different output if you wanted to test for a failure or something.
Function Test-writeHost
{
Add-Content 'C:\TEMP\duplicates.txt' 'The Message'
}
Describe "Test write" {
It "Test write" {
Test-writeHost
Mock Get-Content -MockWith { 'Clearly not the message' }
Get-Content -Path 'C:\TEMP\duplicates.txt' | Should be 'Clearly not the message'
}
}
Somewhat. The question you need to ask yourself is do you want to actually test if your function works or do you want to test if the logic of your function works? Generally what you want to do with Pester is test all of it which means you generally end up with multiple tests for the same function just doing different things.
You probably want to test your logic of your function first by mocking. Then assuming your function is doing something easy to reverse like creating a registry key after your mock tests have passed the your “logic tests” you can then just let the function go off and actually run then test to see if it actually created what you expected it to.
As an example for your particular example I would do something like this
Import-Module "C:\Temp\CreateRegistry.psm1" -Force
Describe "RegistryPath" {
CreateRegistry
It "Creates Registry path" {
(Get-ItemProperty -Path 'HKLM:\Software\WOW6432Node\This is Testing').CreatedDateTime | Should belike ((Get-Date -Format dd/MM/yyyy) + '*')
}
}
However if you wanted to mock it I would do something like this
There’s nothing in your current function that can be mocked, and mocking the whole function itself is pointless. (Your test would basically be saying, at that point, “did I mock the function?”)
What you can do is wrap your calls to the static members of the Environment class in PowerShell functions, then mock those functions. At that point, you will be performing useful tests of your function’s logic (such as does it inject the backslash properly if a domain exists, does it return a value at all, etc.) That code could look like this:
function Get-CurrentUserInfo
{
$domain = Get-DomainName
$user = Get-UserName
if (!([string]::IsNullOrEmpty($domain))) { $domain = $domain + '\' }
$currentUser = $domain + $user
#return $currentUser I have commented out so that it will not return any output
}
function Get-UserName
{
return [Environment]::UserName
}
function Get-DomainName
{
return [Environment]::UserDomainName
}
Now your tests can look like this:
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Get-CurrentUserInfo.ps1"
Describe "CurrentUser" {
Mock Get-UserName { return 'MockUser' }
Context 'When there is no domain name' {
Mock Get-DomainName
It "Returns the proper value" {
Get-CurrentUserInfo | Should be 'MockUser'
}
}
Context 'When there is a domain name' {
Mock Get-DomainName { return 'MockDomain' }
It "Returns the proper value" {
Get-CurrentUserInfo | Should be 'MockDomain\MockUser'
}
}
}