Ejemplo n.º 1
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");
        }
        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");
        }
        static void thatMultipleAnswersAreHandled()
        {
            // Given
            fflib_ApexMocks mocks = new fflib_ApexMocks();
            fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));
            mocks.startStubbing();
            mocks.when(mockList.get(3)).thenAnswer(new fflib_AnswerTest.FirstAnswer());
            mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.SecondAnswer());
            mocks.stopStubbing();

            // When
            mockList.add("one");
            string answer1 = mockList.get(3);
            string answer2 = mockList.get2(0, "Hi hi Hello Hi hi");
            System.assertEquals("this is the first answer", answer1, "the answer wasnt the one expected");
            System.assertEquals("and this is the second one", answer2, "the answer wasnt the one expected");
        }
        private static void whenVerifyCustomMatchersCanBeUsed()
        {
            // Given
            fflib_ApexMocks mocks    = new fflib_ApexMocks();
            fflib_MyList    mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList));

            // When
            mockList.get(1);
            mockList.get(2);
            mockList.get(3);
            mockList.get(4);
            mockList.get(5);

            // Then
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(3))).get((int)fflib_Match.matches(new isOdd()));
            ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).get((int)fflib_Match.matches(new isEven()));
        }
        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);
        }