Exception with mocked object using NUnit and JustMock

2 posts, 0 answers
  1. Neeraj
    Neeraj avatar
    2 posts
    Member since:
    Dec 2014

    Posted 16 Dec 2014 Link to this post

    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.
  2. Stefan
    Stefan avatar
    198 posts

    Posted 17 Dec 2014 Link to this post

    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.


    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.

Back to Top