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

Return mocked object from ctor?

3 Answers 31 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 29 Jan 2015, 10:44 PM
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







3 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 30 Jan 2015, 02:22 PM
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.

 
0
David
Top achievements
Rank 1
answered on 30 Jan 2015, 08:00 PM
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
0
Stefan
Telerik team
answered on 02 Feb 2015, 07:41 AM
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.

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