This release focuses primarily on making assertions better, even thought there are other small fixes and updates.
What's new since 4.1.1:
New assertions
-
There are now
Should -BeLessThan
andShould -BeLessOrEqual
assertions to complement the existingBeGreaterThan
andBeGreaterOrEqual
assertions. -
Should -Contain
assertion was added that operates on collections, the same way the-contains
operator operates on them.
1,2,3 | Should -Contain 1
Should -HaveCount
was added that counts items in a collection
1,2,3 | Should -HaveCount 3
Should -BeTrue
andShould -BeFalse
were added to assert on truthy and falsy values
$true | Should -BeTrue
1 | Should -BeTrue
$false | Should -BeFalse
0 | Should -BeFalse
Assertion improvements
- Every built in assertion allows you to specify
-Because
parameter, to give more meaning to your tests.
function Get-Group { $null }
$groups = 1..10 | Get-Group -Size 3
$groups | Should -HaveCount 4
`-Because "because 10 items are split into three groups with 3 members and one extra group with 1 member"
Which fails with:
Expected a collection with size {4},
because 10 items are split into three groups with 3 members and one extra group with 1 member,
but got collection with size {1} [].
Should -Throw
has-ExceptionType
parameter that matches the exception type and any of its subtypes.
function a () { throw [System.ArgumentNullException]"argument is null" }
# this passes because, ArgumentNullException inherits from ArgumentException
{ a } | Should -Throw -ExceptionType ([ArgumentException])
# this fails, because ArgumentNullExcption does not inherit from InvalidOperationException
{ a } | Should -Throw -ExceptionType ([InvalidOperationException])
Should -Throw
has-PassThru
parameter that returns the caputed exception to output. This is useful for catching a complex exception and further inspecting it.
$err = {
throw (New-Object System.InvalidOperationException "Something wrong happened",
(New-Object DivideByZeroException "you divided by zero")
)
} | Should -Throw -PassThru
$err.Exception.InnerException.Message | Should -Be "you divided by zero"
Should -BeOfType
is now aliased to-HaveType
1 | Should -HaveType [int]
Breaking changes:
Should -Not -Throw
will fail if any exception is thrown. The filters applied onShould -Not -Throw
do not apply, because when an exception is thrown, it automatically means that the test should not pass.