static void thatCaptureAllArgumentsForTheVerifiedMethods() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); List <string> stringList = new List <string> { "3" }; // When mockList.add("Fred"); mockList.add(stringList); mockList.clear(); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)argument.capture()); ((fflib_MyList.IList)mocks.verify(mockList)).add((List <string>)argument.capture()); System.assertEquals(stringList, (List <string>)argument.getValue(), "the argument captured is not as expected"); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(2, argsCaptured.size(), "expected 2 argument to be captured"); System.assertEquals("Fred", (string)argsCaptured[0], "the first value is not as expected"); }
static void thatCaptureAllArgumentswhenMethodIsCalledWithTheSameArgument() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Fred"); mockList.add("Barney"); mockList.add("Wilma"); mockList.add("Barney"); mockList.add("Barney"); mockList.add("Betty"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList, 6)).add((string)argument.capture()); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(6, argsCaptured.size(), "expected 6 arguments to be captured"); System.assertEquals("Fred", (string)argsCaptured[0], "the first value is not as expected"); System.assertEquals("Barney", (string)argsCaptured[1], "the second value is not as expected"); System.assertEquals("Wilma", (string)argsCaptured[2], "the third value is not as expected"); System.assertEquals("Barney", (string)argsCaptured[3], "the fourth value is not as expected"); System.assertEquals("Barney", (string)argsCaptured[4], "the fifth value is not as expected"); System.assertEquals("Betty", (string)argsCaptured[5], "the sixth value is not as expected"); }
static void thatDoesNotCaptureAnythingWhenCaptorIsWrappedInAMatcherWithInOrderVerification() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_InOrder inOrder1 = new fflib_InOrder(mocks, new List <object> { mockList }); // When mockList.add("Same", "Same", "First call", "First call"); mockList.add("Same", "Same", "Second call", "Second call"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(1))).add((string)fflib_Match.allOf(fflib_Match.eqString("Same"), fflib_Match.eqString("Same"), argument.capture()), (string)fflib_Match.allOf(fflib_Match.eqString("Same"), fflib_Match.eqString("Same"), argument.capture()), (string)fflib_Match.allOf(argument.capture(), fflib_Match.eqString("First call")), (string)fflib_Match.allOf(argument.capture(), fflib_Match.eqString("First call"))); List <object> capturedValues = argument.getAllValues(); System.assertEquals(0, capturedValues.size(), "nothing should have been capture because the matcher it not really a capture type, but a allOf()"); System.assertEquals(null, (string)argument.getValue(), "nothing should have been capture because the matcher it not really a capture type, but a allOf()"); }
static void thatCanHandleMultipleCapturesInOneMethodCallWithInOrderVerification() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_InOrder inOrder1 = new fflib_InOrder(mocks, new List <object> { mockList }); // When mockList.add("Fred", "Barney", "Wilma", "Betty"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(1))).add((string)fflib_Match.eq("Fred"), (string)argument.capture(), (string)argument.capture(), (string)fflib_Match.eq("Betty")); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(2, argsCaptured.size(), "expected 2 argument to be captured"); System.assertEquals("Barney", (string)argsCaptured[0], "the first value is not as expected"); System.assertEquals("Wilma", (string)argsCaptured[1], "the second value is not as expected"); }
static void thatCaptureAllArgumentsFromMultipleMethodsWithInOrderVerification() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_InOrder inOrder1 = new fflib_InOrder(mocks, new List <object> { mockList }); // When mockList.add("Fred"); mockList.add("Barney"); mockList.get2(3, "pebble"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(2))).add((string)argument.capture()); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(1))).get2((int)fflib_Match.eq(3), (string)argument.capture()); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(3, argsCaptured.size(), "expected 3 argument to be captured"); System.assertEquals("Fred", (string)argsCaptured[0], "the first value is not as expected"); System.assertEquals("Barney", (string)argsCaptured[1], "the second value is not as expected"); System.assertEquals("pebble", (string)argsCaptured[2], "the third value is not as expected"); }
static void thatCaptureAllArgumentsWithInOrderVerification() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_InOrder inOrder1 = new fflib_InOrder(mocks, new List <object> { mockList }); // When mockList.add("Fred"); mockList.add("Barney"); mockList.add("Wilma"); mockList.add("Betty"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(4))).add((string)argument.capture()); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(4, argsCaptured.size(), "expected 4 argument to be captured"); System.assertEquals("Fred", (string)argsCaptured[0], "the first value is not as expected"); System.assertEquals("Barney", (string)argsCaptured[1], "the second value is not as expected"); System.assertEquals("Wilma", (string)argsCaptured[2], "the third value is not as expected"); System.assertEquals("Betty", (string)argsCaptured[3], "the forth value is not as expected"); }
static void thatDoesNotCaptureIfNotVerified() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("3"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(List <string>)); ((fflib_MyList.IList)mocks.verify(mockList, fflib_ApexMocks.NEVER)).add((List <string>)argument.capture()); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(0, argsCaptured.size(), "expected 0 argument to be captured"); System.assertEquals(null, argument.getValue(), "no value should be captured, so must return null"); }
static void thatCaptureArgumentOnlyFromVerifiedMethod() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Fred"); //the next call should be ignored because is not the method that has under verify, //even if have the same type specified in the capturer. mockList.addMore("Barney"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)argument.capture()); System.assertEquals("Fred", (string)argument.getValue(), "the argument captured is not as expected"); System.assertEquals(1, argument.getAllValues().size(), "the argument captured should be only one"); }