Responsive Ad Area

Share This Post

test

How to mock objects in a C# Windows application for integration testing?

I am working on a Windows Desktop application in C#. The app depends on a Web API to retrieve some data and interacts with some embedded devices via USB. I have used MVVM pattern with the Caliburn.micro framework. I have also included dependency injection using simple container from Caliburn.micro.

What I would like to do is provide stubs for the Web API client and also for the USB devices during development. Reason for API client stub is that the API is not yet completed and I don’t want to depend on it. However, I know the planned endpoints and expected responses from the API. Similarly, I do not have the USB devices available but know the protocol used to communicate with them. My idea is to provide stubs for these classes representing the API, device and complete other parts of the app. Later I can replace the stubs with actual classes.

Looking at various frameworks for mocking objects (moq, autofaq, Rhino.mocks), I am getting lost into how do I put them to use. Most or all of the examples I found about their usage talk about mocking the objects in unit testing rather than the actual code. I would like to actually simulate the object behaviour and test the app in entirety rather than unit testing individual functions.

I am thinking to create duplicate classes in code using the same interface. The duplicate class would simulate the behaviour with some fixed data being returned. When the development is complete, probably the duplicate (simulated) class can be commented out or disabled in the code.


    public class Bootstrapper : BootstrapperBase
    {

        private SimpleContainer _container = new SimpleContainer();

        public Bootstrapper()
        {
            Initialize();
        }

        protected override void Configure()
        {

            _container.Instance(_container);
            _container
                .Singleton<IWindowManager, WindowManager>()
                .Singleton<IEventAggregator, EventAggregator>()
                .Singleton<IAPIHelper, APIHelper>();


            _container
                .PerRequest<IDevice, SprintProDevice>();

            GetType().Assembly.GetTypes()
                .Where(type => type.IsClass)
                .Where(type => type.Name.EndsWith("ViewModel"))
                .ToList()
                .ForEach(viewModelType => _container.RegisterPerRequest(
                    viewModelType, viewModelType.ToString(), viewModelType));

        }
}


APIHelper is the class which will act as client for Web API interaction. So, in my case, I can create one more APIHelper class and use that like shown below.

    _container.Singleton<IAPIHelper, DummyAPIHelper>();

Is it the right way or the only way to do this? Can you suggest some better way, if any to simulate the object behaviour?


How to mock objects in a C# Windows application for integration testing?
How to mock objects in a C# Windows application for integration testing?
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar