Checking method invocation and parameters without mocking structure

I am looking for the best way to verify that a given method (unit) is executing the correct logic.

In this case, I have a method similar to:

public void GoToMyPage()
{
    DispatcherHelper.BeginInvoke(() =>
    {
        navigationService.Navigate("mypage.xaml", "id", id);
    });
}

navigationService- it is injected interface version INavigationService. Now, I want to check in my unit tests what Navigate(...)is being called with the correct parameters.

However, on Windows Phone, IL-radiation is not supported to some extent, when the mocking structure can create a dynamic proxy server and analyze the call. So I need to analyze this manually.

A simple solution would be to store the values ​​invoked by the method Navigate(...)in public properties and test them in unit tests. However, this is pretty tedious of what needs to be done for all kinds of layouts and methods.

So my question is: is there a smarter way to create analytic calls using C # functions (such as delegates), without using a reflection-based proxy and without having to manually save debugging information?

+3
source share
1 answer

My approach would be to manually create a test implementation of the INavigationService that captures calls and parameters and allows you to check them later.

public class TestableNavigationService : INavigationService
{
    Dictionary<string, Parameters> Calls = new Dictionary<string, Parameters>();

    public void Navigate(string page, string parameterName, string parameterValue)
    {
        Calls.Add("Navigate" new Parameters()); // Parameters will need to catch the parameters that were passed to this method some how
    }

    public void Verify(string methodName, Parameters methodParameters)
    {
        ASsert.IsTrue(Calls.ContainsKey(methodName));
        // TODO: Verify the parameters are called correctly.
    }
}

this could be used in your test like this:

public void Test()
{
    // Arrange
    TestableNavigationService testableService = new TestableNavigationService ();
    var classUnderTest = new TestClass(testableService );

    // Act
    classUnderTest.GoToMyPage();

    // Assert
    testableService.Verify("Navigate");
}

, , , .

+3

All Articles