Open johlju opened 2 years ago
Maybe Mock Get-Something1 -Throw
? But let's think about how it would grow. -Throw as a switch is nice, and probably what 90% of the usages would use that. But it would also be nice to be able provide a custom message, and letting -Throw to take $null sucks, because then it will eat your command name when you do Mock -Throw Get-Something. So maybe another parameter -ThrowMessage?. Or -Because?
Mock ... -Throw -Because "we're consistent"
👍
Should this also work with non-default mocks? Like a direct alternative to -MockWith { throw 'invalid parameters used'}
?
Yeah, why not, I don't see myself using it, but I also don't see a reason why not implement it.
Personally I would like to see parametrized mocks failing / doing nothing, rather than failling back to calling the real command when there is no default mock.
I like -Throw
and -Throw -Because
for a custom message. But then it would also be great if we could output the command name, e.g. if we have -Throw -Because 'must be overridden with a template'
it would output something like because command Get-Somthing must be overridden with a template
, or if we could use <_>
in the string to get the command name at that location. 🤔
👍 The command name would be part of static error message. -Because
usually appends an optional message somewhere in the error message. Ex.
Default mock for Get-Something should never be called, because ...
Yes, that. Make it work like assertions. "Default mock for Get-Something was called, <optional because message>,
but it should have never been called.". If you want fully custom message, then throw yourself from the mock body.
The implementer is expected to add the -Throw
parameter to Mock, and throw when the parameter is provided, and the mock is invoked.
It will probably need to be stored as two new fields on the internal mock object, that is stored in mock table. There are no changes to the complicated mock execution part that I can think of, so this change should be rather simple.
Moved to 6, where we want to throw by default, so the requested functionality will be done by default.
Moved to 6, where we want to throw by default, so the requested functionality will be done by default.
@nohwnd: Need more details to implement this. See a concern I have here: https://github.com/pester/Pester/issues/2178#issuecomment-1124679601
Summary of the feature request
As a contributor or maintainer of a project I'd like to be able to help other contributors to correctly mock functions by failing a test if a mock does not exist. This would be very useful in situations when there are very complex scripts that handle a lot of scenarios and using a lot of external helper functions.
The ask would be to add a new parameter to
Mock
that set up a default mock that fails the test if no other mock have overridden it. The parameter name could beAssert
,MustMock
,Override
, orMustBeOverridden
(or any other name that is better).How should it work? (optional)
Take this example as a "complex script", let us say a contributor added the second
Context
-block for a new scenario. The contributor missed to add a mock for the functionGet-Something2
, and the maintainer missed it in the review. The script luckily works, but when the mock was missed the test runs much slower than everyone expects, and does not easily show in the pipeline.This result is a test that runs "slow" (3.45 seconds):
We could help prevent this today by adding a default mock for each command that needs to be mocked by contributors under the
Describe
-block. The default mock throws an error.This result is:
The contributor adds the missing mock and the tests passes and runs as quick one would expect.
The result is faster execution:
Instead of setting a default mock that throws I would like to see this works natively with a command. So instead of this:
This would look nicer and be simple if we could simply replace the BeforeAll-block with this: