First, I know there are a ton of examples of getting around the HttpContext MapPath for unit testing on stack overflow already, but I still feel like my question is unique to what I've seen so far.
I already have an interface for my WebService
public interface IWebService
{
Token GetTokenType(string url, string props, string values, string tokenType);
}
My Production Implementation is:
public class WebServiceImpl : IWebService
{
WebService sts = new WebService("https://example.com", HttpContext.Current.Server.MapPath("~/Config/config.txt"));
public WebServiceImpl()
{
sts.BasicAuthUsername = "username";
sts.BasicAuthPassword = "password";
}
public Token GetTokenType(string url, string props, string values, string tokenType)
{
Token token = sts.GetTokenType(url, props, values, saml);
return token;
}
}
And my Test Implementation is:
public class TestWebServiceImpl : IWebService
{
public WebService sts;
public WebServiceImpl()
{
//This is the line I need to somehow mock or stub or skip..
sts = new WebService("https://example.com", HttpContext.Current.Server.MapPath("~/Config/config.txt"));
sts.BasicAuthUsername = "username";
sts.BasicAuthPassword = "password";
}
public Token GetTokenType(string url, string props, string values, string tokenType)
{
Token token = sts.GetTokenType(url, props, values, saml);
return token;
}
}
The problems I've encountered with trying to use dependency injection on the HttpContext MapPath is that the WebService using it as a constructor will not accept any path I give it. Additionally, I can't see into this WebService class, but I believe it is going to try and make a call to the url even if I can get it to read the file. I also believe that it defeats the purpose of the unit test if I'm using it.
Therefore, my question is, can I mock the WebService within the interface (or some other way) so that I don't even have to use the MapPath and my WebService mock is good to go? Everything after this can be stubbed simply enough, it is just getting past this pesky MapPath.
A few things I've tried: Creating a mock object within the interface:
Mock<WebService> webService = new Mock<WebService>(bogus, constructor)
Stubbed the getTokenType() but still needed to instantiate the WebService.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire