예제 #1
0
        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");
        }
예제 #2
0
        static void thatAnswerOnlyForTheMethodStubbedWithAnswer()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            mocks.startStubbing();
            mocks.when(mockList.get(3)).thenReturn("ted");
            mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.BasicAnswer());
            mocks.stopStubbing();

            // When
            mockList.add("one");
            string noAnswered = mockList.get(3);

            mockList.get2(0, "Hi hi Hello Hi hi");

            // Then
            object methodCalled = actualInvocation.getMethod();

            System.assert(methodCalled is fflib_QualifiedMethod, "the object returned is not a method as expected");
            string expectedMethodSignature = fflib_MyList.getStubClassName() + ".get2(Integer, String)";

            System.assertEquals(expectedMethodSignature, ((fflib_QualifiedMethod)methodCalled).toString(), " the method is no the one expected");
            System.assertEquals("ted", noAnswered, "the get method should have returned the stubbed string");
        }
예제 #3
0
        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");
        }
예제 #4
0
        static void thatCanPerformFurtherAssertionsOnCapturedArgumentValue()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            //When
            TestInnerClass testValue = new TestInnerClass();

            testValue.i = 4;
            testValue.s = "5";
            mockList.set(1, testValue);

            //Then
            fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(TestInnerClass));

            ((fflib_MyList.IList)mocks.verify(mockList)).set(fflib_Match.anyInteger(), argument.capture());
            object capturedArg = argument.getValue();

            System.assertNotEquals(null, capturedArg, "CapturedArg should not be null");
            System.assert(capturedArg is TestInnerClass, "CapturedArg should be SObject, instead was " + capturedArg);
            TestInnerClass testValueCaptured = (TestInnerClass)capturedArg;

            System.assertEquals(4, testValueCaptured.i, "the values inside the argument captured should be the same of the original one");
            System.assertEquals("5", testValueCaptured.s, "the values inside the argument captured should be the same of the original one");
        }
        private static void thatBetweenThrownExceptionIfCalledMoreThanAtMostNumberOfTimesWithMatchers()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob");
            mockList.add("fred");
            mockList.add("bob");
            mockList.add("fred");
            mockList.add("fred");
            mockList.add("fred");

            // Then
            try
            {
                ((fflib_MyList.IList)mocks.verify(mockList, mocks.between(3, 5))).add(fflib_Match.anyString());
                System.assert(false, "an exception was expected because we are asserting that the method is called 5 times when instead is called six times");
            }
            catch (fflib_ApexMocks.ApexMocksException ex)
            {
                string expectedMessage = "Expected : 5 or fewer times, Actual: 6 -- Wanted but not invoked: " + fflib_MyList.getStubClassName() + ".add(String).";
                string actualMessage   = ex.getMessage();
                System.assertEquals(expectedMessage, actualMessage,
                                    "the exception has been caught as expected, however the message is not as expected");
            }
        }
예제 #6
0
        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");
        }
예제 #7
0
        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");
        }
        private static void thatCustomMessageIsAdded()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob");
            mockList.add("bob");
            mockList.add("bob");
            string customAssertMessage = "Custom message to explain the reason of the verification";

            // Then
            try
            {
                ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2).description(customAssertMessage))).add(fflib_Match.anyString());
                System.assert(false, "an exception was expected");
            }
            catch (Exception exc)
            {
                string exceptionMessage = exc.getMessage();
                string expectedMessage  = string.format(BASIC_VERIFY_ASSERTION_MESSAGE, new List <string> {
                    "2", "3"
                }) + fflib_MyList.getStubClassName() + ".add(String). " + customAssertMessage + ".";
                System.assertEquals(expectedMessage, exceptionMessage,
                                    "The exception was caught, but the message was not as expected. " +
                                    "Expected: [" + expectedMessage + "],  Actual: [" + exceptionMessage + "].");
            }
        }
예제 #9
0
        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()");
        }
예제 #10
0
        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 thatAnswerToVoidAndNotVoidMethods()
        {
            // Given
            fflib_ApexMocks mocks = new fflib_ApexMocks();
            fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));
            mocks.startStubbing();
            ((fflib_MyList)mocks.doAnswer(new fflib_AnswerTest.FirstAnswer(), mockList)).get(3);
            ((fflib_MyList)mocks.doAnswer(new fflib_AnswerTest.BasicAnswer(), mockList)).addMore("Hi hi Hello Hi hi");
            ((fflib_MyList)mocks.doAnswer(new fflib_AnswerTest.SecondAnswer(), mockList)).get2(4, "Hi hi Hello Hi hi");
            mocks.stopStubbing();

            // When
            string answer1 = mockList.get(3);
            string answer2 = mockList.get2(4, "Hi hi Hello Hi hi");
            mockList.addMore("Hi hi Hello Hi hi");

            // Then
            object methodCalled = actualInvocation.getMethod();
            System.assert(methodCalled is fflib_QualifiedMethod, "the object returned is not a method as expected");
            string expectedMethodSignature = fflib_MyList.getStubClassName()+ ".addMore(String)";
            System.assertEquals(expectedMethodSignature, ((fflib_QualifiedMethod)methodCalled).toString(),
			"the last method called should be the addMore, so should be the last to set the actualInvocation variable.");
            System.assertEquals("this is the first answer", answer1, "the answer was not the one expected");
            System.assertEquals("and this is the second one", answer2, "the answer was not the one expected");
        }
        private static void thatThrownExceptionIfCalledLessThanAtLeastOnce()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob");
            mockList.add("fred");
            mockList.add("bob");

            // Then
            try
            {
                ((fflib_MyList.IList)mocks.verify(mockList, mocks.atLeastOnce())).add("rob");
                System.assert(false, "an exception was expected because we are asserting that the method is called one times when instead is not called");
            }
            catch (fflib_ApexMocks.ApexMocksException ex)
            {
                string expectedMessage = "Expected : 1 or more times, Actual: 0 -- Wanted but not invoked: " + fflib_MyList.getStubClassName() + ".add(String).";
                string actualMessage   = ex.getMessage();
                System.assertEquals(expectedMessage, actualMessage,
                                    "the exception has been caught as expected, however the message is not as expected");
            }
        }
        private static void verifyMethodCallWhenNoCallsBeenMadeForType()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).add("bob");
        }
예제 #14
0
        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(List <string> .class);
        static void thatExceptionIsThrownWhenAccessNegativeIndexArgument()
        {
            // Given
            fflib_ApexMocks mocks = new fflib_ApexMocks();
            fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));
            mocks.startStubbing();
            mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.ExceptionForNegativeArgumentIndex());
            mocks.stopStubbing();

            // When
            string actualValue = mockList.get2(0, "Hi hi Hello Hi hi");
        }
        static void thatArgumentListEmptyForMethodWithNoArgument()
        {
            // Given
            fflib_ApexMocks mocks = new fflib_ApexMocks();
            fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));
            mocks.startStubbing();
            mocks.when(mockList.isEmpty()).thenAnswer(new fflib_AnswerTest.ArgumentListEmptyForMethodWithNoArgument());
            mocks.stopStubbing();

            // When
            bool actualValue = mockList.isEmpty();
        }
        private static void whenVerifyMethodNeverCalledMatchersAreReset()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).get(fflib_Match.anyInteger());
            ((fflib_MyList.IList)mocks.verify(mockList)).add(fflib_Match.anyString());
        }
        private static void verifyMultipleMethodCallsWithSameSingleArgument()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob");
            mockList.add("bob");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).add("bob");
        }
        private static void thatVerifiesAtLeastOnceNumberOfTimesWithMatchers()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob");
            mockList.add("fred", "fred", "fred", "fred");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.atLeastOnce())).add(fflib_Match.anyString());
        }
        private static void verifyMethodNotCalled()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.get(0);

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).add("bob");
            ((fflib_MyList.IList)mocks.verify(mockList)).get(0);
        }
        private static void verifySingleMethodCallWithMultipleArguments()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.set(0, "bob");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList)).set(0, "bob");
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).set(0, "fred");
        }
        private static void verifyTimesMethodHasBeenCalledWithMatchers()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob1");
            mockList.add("bob2");
            mockList.add("bob3");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(3))).add(fflib_Match.anyString());
        }
        private static void verifyTimesMethodHasBeenCalled()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob");
            mockList.add("bob");
            mockList.add("bob");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(3))).add("bob");
        }
        private static void verifyNeverMethodHasNotBeenCalled()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob1");
            mockList.add("bob2");
            mockList.add("bob3");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).add("bob");
        }
        private static void verifyNeverMethodHasBeenNotCalledWithMatchers()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("fred", "fred", "fred", "fred");
            mockList.add("fred", "fred", "fred", "fred");
            mockList.add("fred", "fred", "fred", "fred");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).add(fflib_Match.anyString());
        }
        private static void whenVerifyMultipleCallsWithMatchersShouldReturnCorrectMethodCallCounts()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob");
            mockList.add("fred");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).add(fflib_Match.anyString());
            ((fflib_MyList.IList)mocks.verify(mockList)).add("fred");
            ((fflib_MyList.IList)mocks.verify(mockList)).add(fflib_Match.stringContains("fred"));
        }
        private static void thatVerifiesAtLeastOnce()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("bob");
            mockList.add("fred");
            mockList.add("bob");
            mockList.add("bob");

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.atLeastOnce())).add("bob");
        }
예제 #28
0
        static void thatArgumentValueIsCaptured()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.add("Fred");

            // 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");
        }
        static void thatMethodsParametersAreAccessibleWhenCalledWithMatchers()
        {
            // Given
            fflib_ApexMocks mocks = new fflib_ApexMocks();
            fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));
            mocks.startStubbing();
            mocks.when(mockList.get2(fflib_Match.anyInteger(), fflib_Match.anyString())).thenAnswer(new fflib_AnswerTest.ProcessArgumentAnswer());
            mocks.stopStubbing();

            // When
            string actualValue = mockList.get2(0, "Hi hi Hello Hi hi");

            // Then
            System.assertEquals("Bye hi Hello Bye hi", actualValue, "the answer is not correct");
        }
        static void thatStoresMockInstanceIntoInvocationOnMock()
        {
            // Given
            fflib_ApexMocks mocks = new fflib_ApexMocks();
            fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));
            mocks.startStubbing();
            mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.BasicAnswer());
            mocks.stopStubbing();

            // When
            string mockCalled = mockList.get2(0, "Hi hi Hello Hi hi");

            // Then
            System.assert(actualInvocation.getMock()is fflib_MyList.IList, "the object returned is not a mock instance as expected");
            System.assertEquals(mockList, actualInvocation.getMock(), "the mock returned should be the mockList used in the stubbing");
        }