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

Need help with Mocking

1 Answer 65 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Neeraj
Top achievements
Rank 1
Neeraj asked on 15 Dec 2014, 06:52 PM
Hi All,We have a requirement where we have to use NUnit to write test methods and JustMock for mocking the application. Dep injOur application is exposing WCF services and the unit tests will be written to test the WCF Operations.We are using VS 2013.In our application we are using Dependency injection while calling the Constructor i.e. the parameters to the Constuctor are created

using Dependency injection.Pls refer below
public NetworkService(IDbContextFactory<NetworkDbContext> contextFactory, IEventBus eventBus, HostService hostService, UserManagementService userManagementService)
        {
            _contextFactory = contextFactory.ThrowIfNull("contextFactory");
            _eventBus = eventBus.ThrowIfNull("eventBus");
            _hostService = hostService.ThrowIfNull("hostService");
            _userManagementService = userManagementService.ThrowIfNull("userManagementService");
      }

I tried mocking using Telerik Justmock to create the objects of ContextFactory/EventBus etc but the object create using mocking was of type Proxy of actual object rather than the object itself i.e. Proxy of IEventBus rather than IEventBus.The issue with this is, the _contextFactory object is used to create a context like below
using (var ctx = _contextFactory.Create())

But as the object is of type Proxy(Object) I am unable to create the context ctx in above scenario and hence the testing fails.

Can anyone please help me with the mocking.

Any help will be highly appreciated.

Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 17 Dec 2014, 11:36 AM
Hello Neeraj,

The entire point of using JustMock is to create mock objects, i.e. objects that look just like the real objects, i.e. they implement the same interfaces, derive from the same base class, but the don't have an actual implementation. If you wanted to retain the actual implementation, you wouldn't need a mock - you would just use the actual type, instead.

I'm guessing here that you don't want to use the real implementation, but you still want to configure the mock object's behavior. To do that you need to arrange them. For example:
var mockContext = Mock.Create<Context>();
Mock.Arrange(() => contextFactory.Create()).Returns(mockContext);
In the above I arrange the return value of contextFactory.Create() to always be mockContext. I can then arrange the mockContext additionally to further configure its behavior as necessary for the test to succeed.

We have a great learning resource for JustMock and mocking in general located here. I suggest that you peruse it to deepen your understanding of mocking and TDD in general. Your question should be answered in sufficient depth in "Day 12" of the series.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

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