字符串数据:
string restResponse = "{'name' : 'Test McGee'}";
想要将上述字符串转换为IRestResponse
类型。
因为:,使用以下代码(第3行)我收到错误:
无法将字符串数据转换为IRestResponse类型。
x.GetRestResponse
返回IRestResponse
类型(来自Restsharp dll)
代码是:
var mockHttpClient = new Mock<IHttpClient>();
string restResponse = "{'name' : 'Test McGee'}";
mockHttpClient.Setup(x => x.GetRestResponse()).Returns(restResponse); //line number 3
答案 0 :(得分:0)
正如评论中已经指出的那样,您可以实例化从IRestResponse
IRestResponse response = new RestResponse { Content = restResponse};
Moq也可用于使用界面
模拟响应var mockHttpClient = new Mock<IHttpClient>();
var content = "{'name' : 'Test McGee'}";
var response = Mock.Of<IRestResponse>(x => x.Content == content);
mockHttpClient.Setup(x => x.GetRestResponse()).Returns(response);