mercredi 4 mars 2015

Using Moq to create a mock the type with constructor

I'm just getting started on unit testing on a simple method and I'm stuck on the below line (specifically the web service object):



public string GetToken()
{
WebService ws = new WebService("https://example.com/", HttpContext.Current.Server.MapPath("~/Config/agent.txt"));

Token token = ws.GetToken(name, values, stuff);
}


At first I tried to use an interface to mock the HttpContext call, but I had trouble going that route, so I'm wondering if I can just Mock the web service? Something like below, but then I don't know how to actually 'inject' this Mock object into the getToken method/class.



var webServiceMock = new Mock<WebService>("http://test.example.com", "/test/file/path");
//maybe something along this syntax?
webServiceMock.Setup(it => it.CreateGetMethod(uri)).Returns(mockWebService.Object);

Aucun commentaire:

Enregistrer un commentaire