apex-enterprise-patterns / fflib-apex-mocks

An Apex mocking framework for true unit testing in Salesforce, with Stub API support
BSD 3-Clause "New" or "Revised" License
423 stars 214 forks source link

Add support for ANY parameter #7

Closed jondavis9898 closed 8 years ago

jondavis9898 commented 8 years ago

Currently, method arguments must match exactly in order to be verified or have the proper result return. Often times, the need arises to verify a method is called 1 or more times regardless of input or have a value returned regardless of input.

Would be nice to be able to setup a mock method that will accept any argument(s) and/or verify a method was called N times without requiring an exact match on the method arguments.

Setup Example: mocks.when(myMock.myMethod(fflib_ApexMocks.ANY, fflib_ApexMocks.ANY)).thenReturn(5);

Verification Example: ((IMyInterface)mocks.verify(myMock, 3).myMethod(fflib_ApexMocks.ANY, fflib_ApexMocks.ANY)

afawcett commented 8 years ago

@frup42 i think we've added this to our internal copy yes? If so, is this part of the changes we discussed to merge back into the public version?

dfruddffdc commented 8 years ago

Yes, we have added matcher support to the internal copy but the PR is still open. I intend to port it over to this repo once merged.

@jondavis9898 FYI, this will allow syntax like this: fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList.IList mockList = new fflib_Mocks.Mockfflib_MyList(mocks);

mocks.startStubbing(); mocks.when(mockList.get((Integer)fflib_Match.isNull())).thenReturn('Null'); mocks.when(mockList.get(fflib_Match.anyInteger())).thenReturn('Any'); mocks.when(mockList.get(fflib_Match.integerMoreThan(3))).thenReturn('>3'); mocks.stopStubbing();

System.assertEquals('Null', mockList.get(null)); System.assertEquals('Any', mockList.get(2)); System.assertEquals('>3', mockList.get(164752));

Matchers support extends to stubbing, verifying and throwing exceptions. You will be able to combine matchers, and easily create your own. Watch this space!

jondavis9898 commented 8 years ago

@frup42 This is AWESOME! I was going to mention things like type specificity (Integer.ANY, etc.) but didn't want to extend the wish list too far initially. What you are describing is exactly what I was hoping for in an ideal case and even more (e.g. integerMoreThan, etc.). Can't wait, thank you!

afawcett commented 8 years ago

:+1:

dfruddffdc commented 8 years ago

:) https://github.com/financialforcedev/fflib-apex-mocks/pull/11