Skip to content

Instantly share code, notes, and snippets.

@nohwnd
nohwnd / gist:ef4f38fbf35aaa012fab
Last active August 29, 2015 14:20
Likeness concept for Pester
function Assert-ValueObjectAreAlike ($ReferenceObject, $DifferenceObject, $ExcludeProperty)
{
[string[]] $properties = ($ReferenceObject.psObject.Properties.Name + $DifferenceObject.psObject.Properties.Name ) |
sort -Unique |
where { $_ -notlike $ExcludeProperty }
-not ( Compare-Object -ReferenceObject $ReferenceObject -DifferenceObject $DifferenceObject -Property $properties )
}
#usual use case
@nohwnd
nohwnd / gist:0eb191e16102bbdf5b7d
Created May 4, 2015 12:20
Mocking Write-Warning
function DoSomething
{
Write-Warning "problem with the server certificate"
}
Describe 'Test warning' {
Mock Write-Warning { $Message }
It 'Writes warning' {
@nohwnd
nohwnd / example.tests.ps1
Created November 5, 2015 21:26
Pester problem matcher for VS Code
describe "DS"{
it "skipped" {} -skip
it "pending" {} -Pending
It "throw" { throw "sdfad" }
It "assertion fail" {1 | should be 10 }
It "string assertion fail" {"asdf" | should be "ffsad" }
it "success" {"adsf"}
}
@nohwnd
nohwnd / mongo_cluster.ps1
Created March 27, 2016 09:00
Rough script to quickly start cluster of local mongoDb instances, for experimenting with elections and sharding.
function Add-MongoToPath () {
$mongoPath = "C:\Program Files\MongoDB\Server\3.2\bin"
if ($env:Path -notlike "*$mongoPath*")
{
$env:path += ";$mongoPath"
}
}
function Start-ServerInstace ([string]$ComputerName = "localhost", [int]$Port, [string]$ReplicationSet = "rs0", [string]$DatabasePath) {
$process = Start-Process Powershell.exe -PassThru -ArgumentList "-NoProfile", "-Command ""
@nohwnd
nohwnd / DescribeInFunction.ps1
Created April 22, 2016 17:30
Run description in function
function SomeFunction ([ScriptBlock] $ScriptBlock)
{
Describe "Why?" {
$result = &$ScriptBlock
$result|Should Be 10
}
}
SomeFunction { 9 }
@nohwnd
nohwnd / TestingStartService.ps1
Last active May 25, 2016 00:50
Testing start service
function EnsureServiceStarted {
param($Name)
Start-Service -name $name
$service = Get-Service -Name $name
$service.Status -eq [ServiceProcess.ServiceControllerStatus]::Running
}
Describe "Ensure service is started" {
It "Started service returns true" {
mock start-service {}
@nohwnd
nohwnd / Faking_Static_Call_in_Pester.ps1
Created May 20, 2016 13:25
Faking call to external static method in Pester
add-type -TypeDefinition @"
using System;
public static class KeyboardEventTestUtil {
public static string keybd_event(byte bVk, byte bScan, UInt32 dwFlags, System.UIntPtr dwExtraInfo) {
return string.Format("{0}:{1}:{2}:{3}", bVk,bScan,dwFlags,dwExtraInfo);
}
}
"@
describe "t" {
function Test-UserName ($Name)
{
$Name -match "^[a-z]+$"
}
Test-UserName $null
Test-UserName ""
Test-UserName "_abc"
Test-UserName "abc1"
Test-UserName "abc "
@nohwnd
nohwnd / CreateTagCategory.ps1
Last active January 15, 2017 18:59
Create tag category
$r = "c:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Sdk\VMware.VimAutomation.Sdk.Types.dll",
"c:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Core\VMware.VimAutomation.ViCore.Types.dll"
Add-Type -ReferencedAssemblies $r -typeDefinition "
using VMware.VimAutomation.ViCore.Types.V1;
using VMware.VimAutomation.ViCore.Types.V1.Tagging;
namespace Mocks
{
@nohwnd
nohwnd / Should-Be.ps1
Last active February 28, 2017 14:48
Should Be NullOrEmpty
function Should-Be {
param($Expected,[Parameter(ValueFromPipeline)]$Actual)
"We are comparing if two values are equal so"
"when `$Expected is $($Expected.GetType()) with value '$Expected'"
if ($null -eq $Actual)
{
"and `$Actual is `$null then the result is:"
}
else