pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.08k stars 470 forks source link

Create assertion(s) for checking state of errors #452

Closed splatteredbits closed 3 years ago

splatteredbits commented 8 years ago

I currently have custom Assert-Error and Assert-NoError functions. I'd like an equivalent in Pester. Something like:

Assert-That -ThereAreNoErrors

Assert-That -LastError -Matches 'not found'
Assert-That -FirstError -Matches 'not found'
Assert-That -Error 1 -Matches 'not found'

Why not:

# If there is an error, the failure doesn't show you what the error was. Assert-That could show you what the unexpected error was.
$Error.Count | Should Be 0

# If there isn't an error, you get indexing error instead of a failed assertion
$Error[0] | Should Match 'not found'

Also, you can't actually use $Error, since that collection is scoped. You have to actually use $Global:Error. That can be hard to remember. Encapsulating that in an assertion would be nice.

Should I create an Assert-That function? Or put these assertions somewhere else? Essentially, I'd like a place for assertions about the system, not necessarily specific objects.

dlwyatt commented 8 years ago

Personally, I tend to use Should Throw in conjunction with -ErrorAction Stop to do these sorts of tests.

From a general UI standpoint, having an Assert-That function instead of Should would be more flexible (and would avoid some headaches that come from using the pipeline as input to Should). Assert-MockCalled and Assert-VerifiableMocks basically exist for that same reason; it wouldn't make any sense to try to shoehorn them into the current implementation of Should. I could see an Assert-That command being an eventual replacement for all three of those commands.

However, I would not want to start that work right now from the master branch, because significant changes have already been made to Should in the https://github.com/pester/Pester/tree/DevelopmentV4 branch. Doing anything out in master would result in another giant merge conflict for me to sort out later, which I'm getting rather sick of doing. I want to get v4 done and released soon. :)

it-praktyk commented 7 years ago

The related project by @nohwnd -> https://github.com/nohwnd/Assert

@dlwyatt, should we close the current issue as 'Feature - refused'?

nohwnd commented 3 years ago

@splatteredbits does this still sound like a good idea to you?