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

Mocking an object instanciation

1 Answer 52 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vincent
Top achievements
Rank 1
Vincent asked on 12 Jan 2011, 05:20 PM

Hi,

I want to create a unit test for the CheckUser method presented below...  In that method we create a new instance of a class named LoginService.  Inside my unit test, I know how to arrange my LoginService instance so that the mocking framework will replace the original call to ValidateUser with a simple "Return 1".  That is not my problem....  My problem is that the parameterless constructor of the LoginService class used in CheckUser does a couple things I'd prefer not to do in my unit test like (as an example) connect to an Active Directory.  Of course, if my unit test tries systematically to create a REAL LoginService then it will always fail as I have no active directory ready for my tests...  Is there a way with JustMock to tell the mocking infrastructure that whenever my code tries to instanciate a class of type LoginService then I want a MOCK (or proxy) of LoginService to be created instead?  On this mock I would of course have set arrange statements to have it behave like I want, let's say, Return 1 when ValidateUser is invoked on the Mock (or proxy)...

public class LegacyCode
{
    
public int CheckUser(string userName, string password)
    {
      ---->  
LoginService _service = new LoginService();
        
        [some code ommited here]
        
        return _service.ValidateUser(userName, password);
    }
}

I know some other mocking products out there support such a feature and I want to know if JustMock supports it also?

Thanks for your time.

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 13 Jan 2011, 11:56 AM
Hi Vincent,

Thanks again for finding time in making the post. A similar answer has been posted to the following thread:

http://www.telerik.com/community/forums/justmock/general-discussions/future-objects-support.aspx

Please check it out.

Kind Regards,
Ricky
the Telerik team
Browse the videos here>> to help you get started with JustMock
Tags
General Discussions
Asked by
Vincent
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or