Modify Should Throw to use dot-source operator#514
Modify Should Throw to use dot-source operator#514dlwyatt wants to merge 1 commit intopester:masterfrom
Conversation
|
I think that we should rather be getting rid of the Describe " " {
It 'Properly failed test' {
{ "abc" } | Should Throw
}
It 'Properly failed test' {
2 | Should Be 1
}
It 'Test exception' {
throw
}
It 'Test exception undercover' {
{ throw } | Should Not Throw
}
}If you look at the result of the tests, you will see that all of them are red, but only the first two qualify as the Red which should be followed by Green. The other two are test exceptions, which suggest that your test is broken. Now the problem is that using the The other suggested use case is using it with It 'Passing exception' {
$ex = { Throw [InvalidOperationException]"Sorry!" } | Should Throw -PassThru
$ex.Message | Should Be "Sorry!"
}So to me it would make more sense to improve error messages for all exceptions, not just those decorated with Should Not Throw. (I will have a look on it this week hopefully.). And not merge this change, as it adds more confusion to behavior that is already confusing for many people. What do you think? |
|
I'm not sure I agree with taking away features just because they don't fit with a particular development method. Not everyone practices TDD, myself included. Personally, I've always thought of Pester simply as a test framework, not as a "TDD" or "BDD" framework. What's most important is that you have tests with decent coverage, not what order you happened to write the code. In any case, we can't get rid of |
|
If throwing any exception would give you as nice output as Should Not Throw, would there be any reason to use it? The order in which you write the code does not matter. You can write tests before or after, but you should always make your test fail in the assertion to prove that the assertion will fail when incorrect results are output. I understand the technical limitations, it was just a suggestion. Because dot-sourcing will change the behavior for all the legitimate uses of Should Throw, and there you don't need the result. |
I have no idea. :) That's a personal preference thing; I'd have to get a feel for both options first. Right now, I feel like the "unhandled" exception output is packed with useful information (stack trace, etc), because many people seem to want to be able to troubleshoot an error based purely on the console output from their CI server. Me, I'm usually happy with the shorter output from I'm not sure where the middle ground is there, unless we start setting up preference variables and/or parameters to |
|
I do like the idea of being able to both checks my concern with Since the FullyQualifiedErrorId is immutable I'd be much happier if I could do |
|
Can't change that in v3 without a breaking change, but the changes to $someScriptBlock | Should -Throw -FullyQualifiedErrorId 'PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand' |
|
@dlwyatt I understand that you want to avoid breaking changes, so why don't we add another { $someScriptBlock } | Should ThrowWithFullyQualifiedErrorId 'PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand'
# or
{ $someScriptBlock } | Should ThrowException 'CommandNotFoundException'
This would go in-line with the existing syntax and we could get rid of the tedious synatx that @JamesWTruher mentioned. |
|
You can extend Pester with custom assertions if you want. Otherwise I would suggest you wait for version 4. Adding a very specific new assertion to Pester v3 is imho not the way to go, next week someone else will request |
|
@nohwnd Cool! I wasn't aware of that. I read the article and tried this and it in general works as I expected. However I came across a strange behaviour when using the NOT keyword along with my assertion. When I write Is this something where I got the custom assertion wrong? Or does this not work with Here is the test output from the tests in the Describing Test-ThrowException.Tests
Context Test-ThrowException
[+] Warmup 156ms
[+] AssertionExists 334ms
[+] GettingHelp-ShouldSucceed 47ms
[+] ThrowExceptionWithFullQualifiedExpectedExceptionSucceeds 45ms
[+] ThrowExceptionWithExpectedExceptionSucceeds 43ms
[+] ThrowExceptionWithPartialExpectedExceptionSucceeds 41ms
[+] ThrowExceptionWithRegexExpectedExceptionSucceeds 34ms
[-] ThrowExceptionWithUnexpectedExceptionIsSupposedToFail 40ms
Test was expected to throw exception of type 'CommandNotFoundException', but was not thrown.
58: { 1 / 0 } | Should ThrowException CommandNotFoundException;
at <ScriptBlock>, C:\Github\biz.dfch.PS.Pester.Assertions\src\ThrowException.Tests.ps1: line 58
[+] ThrowExceptionWithoutExpectedExceptionSucceeds 50ms
[-] ThrowExceptionWithoutExceptionIsSupposedToFail 44ms
RuntimeException: Test was supposed to throw exception 'System.DivideByZeroException', but was not thrown.
at PesterThrowException, C:\Program Files\WindowsPowerShell\Modules\biz.dfch.PS.Pester.Assertions\ThrowException.ps1: line 84
at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Assertions\Should.ps1: line 45
at <ScriptBlock>, C:\Github\biz.dfch.PS.Pester.Assertions\src\ThrowException.Tests.ps1: line 70
[-] ThrowExceptionWithoutExceptionShouldSucceedButActuallyFails 52ms
RuntimeException: Test was supposed to throw exception 'System.DivideByZeroException', but was not thrown.
at PesterThrowException, C:\Program Files\WindowsPowerShell\Modules\biz.dfch.PS.Pester.Assertions\ThrowException.ps1: line 84
at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Assertions\Should.ps1: line 45
at <ScriptBlock>, C:\Github\biz.dfch.PS.Pester.Assertions\src\ThrowException.Tests.ps1: line 76Thanks for your reply. Or should I ask this in a separate issue (as this is not really related to the PR)? |
|
@dfch did not notice this until today :/ |
|
Guys, should it be still open? |
|
Most likely not. Won't implement this, it would change the behavior, and should not throw is bad practice. |
Right now, if one wants to both verify whether or not a command throws an exception and test its output at the same time, the code requires a bit of a workaround:
By making the
Should Throwcommand dot-source the script block, any variable assignments inside that block will persist out in the scope of theItblock, allowing for a cleaner approach: