Unit Testing: Dependency Inversion

by Denden   Last Updated October 30, 2017 05:05 AM

I'm trying to start adding some tests to my code. Please take it easy on me cause i'm still new to this stuffs.

As far as i understand dependency inversion, all the dependencies will be place inside a class constructor and they must be an Interface so that we can easily fake it. By following this rule i may end up having a login class like this.

public class Login
{ 
    string _username;
    string _password;
    IUserRepository _userRepository;
    public Login(string username, string password, IUserRepository userRepository)
    {
        this._username=username;
        this._password=password;
        this._userRepository=userRepository;
    }
}

public interface IUserRepository 
{ 

}

This is contrary to how i understand classes will be. I do believe that users of your code need to know nothing about its internal workings. All they need to know is how to instantiate and use the object. In short, provide them a way to get there but hide the details. So to sum it up, having the IUserRepository inside a constructor just made my code more confusing to some users of my class. Please enlighten me on this so that i won't make any more mistakes down the road.

Regards,

Tags : c# unit-testing


Related Questions


What is IBM's CUPRIMDS?

Updated February 18, 2017 10:05 AM

What does stubbing mean in programming?

Updated March 03, 2017 13:05 PM

Integration tests, but how much?

Updated February 28, 2017 08:05 AM

i want a postman's counterpart for mobile developers

Updated December 05, 2017 07:05 AM

How to manage non-unit tests in a project?

Updated April 26, 2017 14:05 PM