Difficulty Mocking Private Generic Method

3 posts, 0 answers
  1. Lee
    Lee avatar
    2 posts
    Member since:
    Nov 2015

    Posted 30 Nov 2015 Link to this post

    Hi all,

     I have the following generic method in my class:

     

    1.private T ValidateApiResponse<T>(Document response) where T : ResponseBase
    2.        {
    3.        }

     My test method looks like this:

    01.[TestMethod]
    02.        public void CardCaptureRequest_CallsCreateRequest()
    03.        {
    04.            //Arrange
    05.            var methodCalled = false;
    06.            var facadeSettings = MockFacadeSettings("http://localhost", "logfile", 1, 2, "proxy");
    07.            var authSettings = MockAuthenticationSettings("clientName", "password");
    08.            var facade = Mock.Create<DatacashFacade>(Behavior.CallOriginal, new object[] { facadeSettings, authSettings });
    09.            var document = Mock.Create<Document>();
    10.            var ccResponse = new CardCaptureResponse
    11.            {
    12.                Status = 1,
    13.                DatacashReference = "refNo",
    14.                Information = "Info",
    15.                MerchantReference = "merchRef",
    16.                RedirectUrl = "http://localhost/",
    17.                Success = true
    18.            };
    19. 
    20.            Mock.NonPublic.Arrange<Document>(facade, "CreateRequest")
    21.                .DoInstead(() =>
    22.                {
    23.                    methodCalled = true;
    24.                }).Returns(document);
    25.            Mock.NonPublic.Arrange<Document>(facade, "SendRequest")
    26.                .IgnoreInstance().Returns(document);
    27. 
    28.            Mock.NonPublic.Arrange<CardCaptureResponse>(facade, "ValidateApiResponse", document)
    29.                 .Returns(ccResponse);
    30. 
    31.            //Act
    32.            facade.CardCaptureRequest(1, "", 2, "", "", "");
    33. 
    34.            //Assert
    35.            Assert.IsTrue(methodCalled);
    36.        }

    Line 28 is attempting to Mock the private method call. 

    However, the test fails with the following:  Method 'ValidateApiResponse' with the given signature was not found on type ....... 

    Any ideas??

     

     

     

  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 01 Dec 2015 Link to this post

    Hello Lee,

    Non-public generic methods must be mocked through the reflection-based overloads, like so:
    var method = typeof(DatacashFacade)
        .GetMethod("ValidateApiResponse", BindingFlags.Instance | BindingFlags.NonPublic)
        .MakeGenericMethod(typeof(CardCaptureResponse));
    Mock.NonPublic.Arrange<CardCaptureResponse>(facade, method, document).Returns(ccResponse);

    I've logged the issue and in future versions of JustMock the non-public API should become more flexible.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. Lee
    Lee avatar
    2 posts
    Member since:
    Nov 2015

    Posted 02 Dec 2015 in reply to Stefan Link to this post

    Thanks Stefan,

     I've gotten around the problem by refactoring my code slightly.

Back to Top