我们可以将字符串数据转换为IRestResponse吗?

时间:2017-06-26 08:37:14

标签: c# moq restsharp

字符串数据:

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

1 个答案:

答案 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);

审核Moq Quick start: Linq to Mocks