我正在尝试找到一种方法来为“CheckSomething”方法编写单元测试,但是我很难弄清楚如何处理我在该方法中实例化的 RestClient 对象。
我应该抽象出RestClient,并将接口依赖项放在类级别并从构造函数内部注入吗?如果我选择这条路线,这是否意味着我必须接口所有依赖项??
public class SomeRepository : IXRepository
{
public SomeRepository()
{
}
public bool CheckSomething()
{
var client = new RestClient("someurl");
var request = new RestRequest("resourceX/{name}", Method.GET);
request.AddUrlSegment("name", "ABC");
var response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.NotFound) return false;
if (response.StatusCode == HttpStatusCode.OK) return true;
throw new Exception("Something is messed up");
}
}