This is a migrated thread and some comments may be shown as answers.

Difficulty Mocking Private Generic Method

2 Answers 172 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Lee
Top achievements
Rank 1
Lee asked on 30 Nov 2015, 10:37 AM

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 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 01 Dec 2015, 08:58 AM
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
0
Lee
Top achievements
Rank 1
answered on 02 Dec 2015, 02:51 PM

Thanks Stefan,

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

Tags
General Discussions
Asked by
Lee
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Lee
Top achievements
Rank 1
Share this question
or