Пример #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 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");
        }
        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();
        }
        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 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");
        }
        static void thatStoresMethodIntoInvocationOnMock()
        {
            // 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
            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");
        }
        static void thatAnswerOnlyForTheStubbedParameter()
        {
            // 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.ProcessArgumentAnswer());
            mocks.stopStubbing();

            // When
            string actualValue1 = mockList.get2(0, "some string for my method");
            string actualValue2 = mockList.get2(0, "Hi hi Hello Hi hi");
            string actualValue3 = mockList.get2(0, "another string for the same method");

            // Then
            System.assertEquals("Bye hi Hello Bye hi", actualValue2, "the answer is not correct");
            System.assertEquals(null, actualValue1, "the answer is not correct");
            System.assertEquals(null, actualValue3, "the answer is not correct");
        }
        static void thatAnswersWithException()
        {
            // 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.ExceptionForAnswer());
            mocks.stopStubbing();

            // When
            try
            {
                mockList.get2(0, "Hi hi Hello Hi hi");
                System.assert(false, "an exception is expected to be thrown on the answer execution");
            }
            catch (fflib_ApexMocks.ApexMocksException ansExpt)
            {
                string expectedMessage = "an error occurs on the execution of the answer";

                // Then
                System.assertEquals(expectedMessage, ansExpt.getMessage(), "the message from the answer is not as expected");
            }
        }