C # Visual Studio Unit Test, choosing client IP
I am writing some unit tests and I am getting an exception coming from my real code when I try to do the following:
string IPaddress = HttpContext.Current.Request.UserHostName.ToString();
Is there a way to tweak the IP without rewriting my code to take the IP as a parameter?
Thanks!
a source to share
Take a look at Dependency Injection .
Basically you run into problems like this when pushing data into a class with (for example in this case) a "context" or "settings" class.
public interface IAppContext
{
string GetIP();
}
After that you have a prod implementation that does the real thing and mock or fake in your tests.
public class AppContext : IAppConext
{
public string GetIP()
{
return HttpContext.Current.Request.UserHostName.ToString();
}
}
The application context gets into the class using the IP address ...
Oops, and as far as I know there are no built-in jokes for any VS release, you will need to check out one of the many - Rhino mocks , Moq ... there are many! Also see typemock , but it takes a different approach.
PK :-)
a source to share