Return mocked object from ctor?

4 posts, 1 answers
  1. David
    David avatar
    9 posts
    Member since:
    Aug 2011

    Posted 29 Jan 2015 Link to this post

    I have this code in my SUT:

                FarmLoggingService tempService = FarmLoggingService.Local;
                if (null != tempService)
                {
                    tempService.Update();
                }
                else
                {
                    tempService = new FarmLoggingService();
                    tempService.Update();

                    if (tempService.Status != SPObjectStatus.Online)
                        tempService.Provision();
                }

    I'm trying to figure out how to mock the call to the FarmLoggingService constructor: tempService = new FarmLoggingService.

    I've tried variations all similar to 
         Mock.Arrange( () => new FarmLoggingService() ).Returns(mockSvc);  

    where mockSvc was created  like this: 
         var mockSvc = Mock.Create<FarmLoggingService>(Constructor.Mocked);

    But nothing seems to be working.  Ultimately, I need to be able to check that Update and Provision were each called once.  I have it working to return null from FarmLoggingService.Local to force me into the else block, mock the return value from the Status property so it always calls Provision, but I can't get the constructor bit working right.

    Any ideas?

    Thanks,

    Dave







  2. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 30 Jan 2015 Link to this post

    Hello David,

    The way you're using the JustMock API is exactly correct. Mocking the return value of "new" expressions was a feature implemented in JustMock 2014 Q3. In fact, using the "Make object ID" feature of the Visual Studio debugger, you should be able to see that the mockSvc and the result of the "new" call are exactly the same instance.

    Can you send me a repro project or a snippet with all the relevant code that demonstrates the issue?

    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.

     
  3. DevCraft R3 2016 release webinar banner
  4. David
    David avatar
    9 posts
    Member since:
    Aug 2011

    Posted 30 Jan 2015 in reply to Stefan Link to this post

    Thanks, Stefan.  I'm using an older version of JustMock, so that must be why it didn't work.  Good to know I had it right, though.

    Dave
  5. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 02 Feb 2015 Link to this post

    Hi David,

    Try it with the the 2014 Q3 version and see if you have any problems.

    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.

     
Back to Top