pester

Testing for mandatory parameters with Pester

醉酒当歌 提交于 2020-08-04 05:04:17
问题 I'm trying to figure out how to have Pester test for parameters that are missing: Find-Waldo.Tests.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' Describe 'Mandatory paramters' { it 'ComputerName' { { $Params = @{ #ComputerName = 'MyPc' ScriptName = 'Test' } . "$here\$sut" @Params } | Should throw } } Find-Waldo.ps1 Param ( [Parameter(Mandatory)] [String]$ComputerName, [String]$ScriptName ) Function

Multiple key press simultaneously for Windows logo key + Alt + PrtScn in powershell?

五迷三道 提交于 2020-05-17 06:54:47
问题 I tried the below code by referred the link but not able to press those keys at a time. Do I need any change? $code = @' namespace SendTheKeys { class SendIt { public static void Main(string[] args) { [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); private const int KEYEVENTF_EXTENDEDKEY = 1; private const int KEYEVENTF_KEYUP = 2; public static void KeyDown(Keys vKey) { keybd_event((byte)vKey,

Pester doesn't catch the thrown error

痴心易碎 提交于 2020-05-15 04:03:49
问题 When I run the following pester test I expect it to catch the expected error but it doesn't. But when I run the test with a different function with a different throw statement it works. Pester Test: Describe "Remove-GenericCredential Function Tests" { $InternetOrNetworkAddress = 'https://PesterTestUser@PesterTestURl.com' Context "Test: Remove-GenericCredential -InternetOrNetworkAddress '$InternetOrNetworkAddress' (Credential does not exist)" { It "This Command threw an error. The credential

How to 'get arguments for calls made on' a Mock in Pester (or otherwise generate a helpful message containing actual and expected values)?

人走茶凉 提交于 2020-01-14 16:53:53
问题 While there are many examples of using Pester to Assert a Mock , I am unable to find good (or any) examples on how to use Pester to get parameters made upon a Mock; this is useful to get a meaningful error message instead of a generic message of so-so usefulness: Expected Invoke-XYZ to be called at least 1 times but was called 0 times Thanks, and obviously; might as well be a "didn't work" SO question. In RhinoMocks + NUnit (C#), for example, one might use code similar to the following to

How to 'get arguments for calls made on' a Mock in Pester (or otherwise generate a helpful message containing actual and expected values)?

Deadly 提交于 2020-01-14 16:51:09
问题 While there are many examples of using Pester to Assert a Mock , I am unable to find good (or any) examples on how to use Pester to get parameters made upon a Mock; this is useful to get a meaningful error message instead of a generic message of so-so usefulness: Expected Invoke-XYZ to be called at least 1 times but was called 0 times Thanks, and obviously; might as well be a "didn't work" SO question. In RhinoMocks + NUnit (C#), for example, one might use code similar to the following to

Not able to fetch the individual details from JSON data

拥有回忆 提交于 2019-12-25 19:06:37
问题 "Ns": { "value": [ { "Nname": "exa", "SR": [ { "name": "port1", "properties": { "description": "Allow port1", "destinationPortRange": "1111", "priority": 100 } }, { "name": "port1_0", "properties": { "description": "Allow port1", "destinationPortRange": "1111", "priority": 150 } }, { "name": "port2", "properties": { "description": "Allow 1115", "destinationPortRange": "1115", "priority": 100, } } ] } ] } Want to assert the details of priority and name but was not able to do it. Here is what I

Compare the properties of two PsCustomObjects

你离开我真会死。 提交于 2019-12-12 03:48:25
问题 I know that I can compare the values of two PowerShell objects: PS> $A = [PsCustomObject]@{"A"=1; "B"=$True; "C"=$False} PS> $B = [PsCustomObject]@{"A"=1; "B"=$False; "C"=$False} PS> Compare-Object $A $B -Property A, B, C A B C SideIndicator - - - ------------- 1 False False => 1 True False <= However, I need to compare the existance the properties of two PowerShell objects. These objects would be considered the same: PS> $A = [PsCustomObject]@{"A"=1; "B"=$True; "C"=$False} PS> $B =

Writing tests for PowerShell functions

筅森魡賤 提交于 2019-12-11 19:13:10
问题 I have the following function (the function is an auxiliary function of an another function and it works correctly): function Get-UserManager { [CmdletBinding()] param ( [pscredential] $credential, [ValidateSet('ID')][string]$searchType, [string]$searchString, ) try { $reply = Invoke-RestMethod -Method Get -Uri $full_uri -Credentia $cred } catch { Write-Verbose "Couldn't connect to the End Point" Write-Debug "$($_.Exception)" return $userListObject } $reply.elements | ForEach-Object { return

How do I mock Read-Host in a Pester test?

て烟熏妆下的殇ゞ 提交于 2019-12-11 03:49:26
问题 If i have this function: Function Test-Foo { $filePath = Read-Host "Tell me a file path" } How do i mock the Read-Host to return what i want? e.g. I want to do something like this (which doesn't work): Describe "Test-Foo" { Context "When something" { Mock Read-Host {return "c:\example"} $result = Test-Foo It "Returns correct result" { $result | Should Be "c:\example" } } } 回答1: This behavior is correct: you should change you're code to Import-Module -Name "c:\LocationOfModules\Pester"

How to do TDD and unit testing in powershell?

强颜欢笑 提交于 2019-11-27 10:54:54
With MS ramming powershell into all new server products, I'm starting to (reluctantly) think I need to take it seriously. Part of "taking it seriously" is TDD. Have you found good methods to unit test power shell scripts? I've found samples of mocking from Mr Geek Noise - but I'd really like something like RhinoMocks . Brian Hartsock has a sample of running tests on powershell strings from MS Test. A little hacky, but it seems to work. What I want is a Powershell TDD experience that is as clean as it is in "real" languages. Update to clarify: The first two answers attempt to steer me away from