Reputation: 197
I have the following legacy class that I want to add some unit tests to using Xunit and Moq
The psuedo code for the class I want to test is below:-
public class Foo : IFoo
{
public StatusResposne GetStatus(string jobId)
{
.
.
var response = GetRequest(doc, targeturl);
.
.
}
private XDocument GetRequest(XDocument doc, string url)
{
}
}
Now i want to test GetStatus but im order to do that i need to mock the GetRequest method.
Can anyone suggest the easiest way to do this?
Cheers
Upvotes: 1
Views: 1260
Reputation: 233135
Unit testing ought to be black-box testing, because otherwise your tests will be too brittle: every time you change an implementation detail, you'll need to fix all the tests that break.
When you view Foo as a black box, only GetStatus
is visible. The fact that there's a private GetRequest
method is irrelevant. Someone else might come by tomorrow and decide to refactor the GetStatus
implementation in such a way that the GetRequest
method is no longer used - perhaps that method will no longer be there.
You should test the observable behaviour of GetStatus
, instead of attempting to test implementation details.
i need to mock the GetRequest method
That's not possible. You can only mock interfaces and accessible virtual methods.
Upvotes: 3
Reputation: 3289
Do you have access to the source code? One way could be to make GetRequest protected virtual and override it in some inherited class and use this class for the unit tests.
Better would be to move this method to some other class and use it as a dependency.
Upvotes: 1