-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable Sequential Mocks #18
Conversation
- Enabled fluent interface for calling `isCalledForTheNthTime` to define different mocked results for repeated method calls - Added examples and tests in `AccountDomainTest` - Retained backwards compatibility with existing implementations - Cleaned up magic strings - Added to .gitIgnore example: ``` mockService.when(mockedMethodName).isCalledForTheNthTime(1).thenReturn(true); mockService.when(mockedMethodName).isCalledForTheNthTime(2).thenReturn(false); ```
3695e01
to
5e09282
Compare
@@ -175,9 +183,18 @@ public with sharing class UniversalMocker implements System.StubProvider { | |||
) { | |||
String keyInUse = this.determineKeyToUseForCurrentStubbedMethod(stubbedMethodName, listOfParamTypes); | |||
this.incrementCallCount(keyInUse); | |||
Integer currentCallCount = this.callCountsMap.get(keyInUse); | |||
Integer countToUse = this.setResultsForCallingNTimes ? currentCallCount : 1; | |||
if (this.setResultsForCallingNTimes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mitchspano What's the reason behind this check for each stubbed method call? Feels like an avoidable performance hit
} | ||
|
||
@IsTest | ||
private static void skipping_indicies_when_setting_is_called_for_the_nth_time_should_cause_invalid_operation() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mitchspano This is an interesting design choice. I'm leaning more towards using the indices as a marker for this call and anything after it till the next explicit index
. For eg: isCalledForTheNthTime(1)
followed by isCalledForTheNthTime(3)
would mean, use the first value for calls 1 and 2 and use the second value for the 3rd call and anything after that. Feels more flexible and also good for when you have an unknown/variable number of method calls and you want to use a different value for the first x
calls and then a common value for all calls after that. Curious to hear your thoughts on this approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the approach! Similar to a different comment, I believe that the method name might need to be updated if we switch the behavior. Perhaps something like isCalledNOrMoreTimes
would make this a little clearer to the reader. We need to think about a naming convention which makes sense for "n or more times" and 0 based indexing 🤔.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point. I think isCalledAfterNthTime
is a bit clearer. It also hints at 0 based indexing.
@@ -228,13 +245,24 @@ public with sharing class UniversalMocker implements System.StubProvider { | |||
} | |||
} | |||
|
|||
private void isCalledForTheNthTime(Integer n) { | |||
if (n == null || n < 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mitchspano I like the fact that this method uses one-based indexing. However, as a result of my, arguably poor, earlier design choice of using zero-based indexing for andInvocationNumber
method for retrieving the method parameters, I'd prefer keeping this consistent and use zero-based indexing. I know its confusing, but I'd hate to change the indexing for andInvocationNumber
at this point and break backwards compatibility :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack. I am not certain that the method name isCalledForTheNthTime
makes sense for the reader if we switched to 0 based indexing. We might need to change the verbiage a little bit.
Test.stopTest(); | ||
|
||
//verify | ||
System.assertEquals(null, myException, 'An exception should not be thrown because both of the Database.SaveResult were successful'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be useful to also print out what the exception was and maybe the stacktracestring to help with debugging efforts.
Addressed in #23 |
isCalledForTheNthTime
to define different mocked results for repeated method callsAccountDomainTest
example: