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

Exception with mocked object using NUnit and JustMock

1 Answer 120 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Neeraj
Top achievements
Rank 1
Neeraj asked on 16 Dec 2014, 01:51 PM
Hi All, We have a requirement where we have to use NUnit to write test methods and JustMock for mocking the application.
Our 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. Please refer below sample
 
 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 created using mocking was of type Proxy of actual object rather than the object itself i.e. Proxy of IEventBus rather than IEventBus.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. Hence the controls goes to the Service but
when it tries to use context object ctx, it throws an "Null Reference exception" and hence the testing fails. 

Can anyone please suggest if there is any issue with the above and suggest on alternate solution using NUnit and JustMock. 

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:39 AM
Hello Neeraj,

This ticket appears to be essentially the same as the one that you posted the day before. Please refer to the answer in the other ticket and see if it answers this one as well, as I think that it does. The takeaway is that you need to use arrangements to specify the behavior of your mock objects.

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