2017-03-07 83 views
2

我想模型的處理RESTClient實現類用於測試目的如何嘲笑RestSharp移植庫在單元測試

public class DataServices : IDataServices 
{ 
    private readonly IRestClient _restClient; 


    public DataServices(IRestClient restClient) 
    { 
     _restClient = restClient; 
    } 

    public async Task<User> GetUserByUserName(string userName) 
    { 
     User user = null; 

     // create a new request 
     var restRequest = new RestRequest("User", Method.GET); 
     // create REST parameters 
     restRequest.AddParameter("userName", userName, ParameterType.QueryString); 
     // execute the REST request 
     var restResponse = await _restClient.Execute<User>(restRequest); 
     if (restResponse.StatusCode.Equals(HttpStatusCode.OK)) 
     { 
      user = restResponse.Data; 
     } 
     return user; 
    } 

} 

我的測試類:

[TestClass] 
public class DataServicesTest 
{ 
    public static IRestClient MockRestClient<T>(HttpStatusCode httpStatusCode, string json) 
    { 
     var mockIRestClient = new Mock<IRestClient>(); 
     mockIRestClient.Setup(x => x.Execute<T>(It.IsAny<IRestRequest>())) 
      .Returns(new RestResponse<T> 
      { 
       Data = JsonConvert.DeserializeObject<T>(json), 
       StatusCode = httpStatusCode 
      }); 
     return mockIRestClient.Object; 
    } 

    [TestMethod] 
    public async void GetUserByUserName() 
    { 
     var dataServices = new DataServices(MockRestClient<User>(HttpStatusCode.OK, "my json code")); 
     var user = await dataServices.GetUserByUserName("User1"); 
     Assert.AreEqual("User1", user.Username); 
    } 
} 

但我不能實例化對象RestResponse ,我有以下錯誤:

.Returns(new RestResponse<T> 
{ 
    Data = JsonConvert.DeserializeObject<T>(json), 
    StatusCode = httpStatusCode 
}); 

Cannot access protected internal constructor 'RestResponse' here.

我該如何解決這個問題?我在Xamarin便攜式庫上使用FubarCoder.RestSharp nuget包。

+0

模擬'IRestResponse '並返回 – Nkosi

回答

3

模擬IRestResponse<T>並返回

public static IRestClient MockRestClient<T>(HttpStatusCode httpStatusCode, string json) 
    where T : new() { 
    var data = JsonConvert.DeserializeObject<T>(json) 
    var response = new Mock<IRestResponse<T>>(); 
    response.Setup(_ => _.StatusCode).Returns(httpStatusCode); 
    response.Setup(_ => _.Data).Returns(data); 

    var mockIRestClient = new Mock<IRestClient>(); 
    mockIRestClient 
     .Setup(x => x.Execute<T>(It.IsAny<IRestRequest>())) 
     .ReturnsAsync(response.Object); 
    return mockIRestClient.Object; 
} 

測試也應更新爲異步以及

[TestMethod] 
public async Task GetUserByUserName() { 
    //Arrange 
    var client = MockRestClient<User>(HttpStatusCode.OK, "my json code"); 
    var dataServices = new DataServices(client); 
    //Act 
    var user = await dataServices.GetUserByUserName("User1"); 
    //Assert 
    Assert.AreEqual("User1", user.Username); 
} 
+0

@Nkosi我試圖用你的方法並且我得到以下錯誤:'T'必須是具有公共無參數構造函數的非抽象類型,才能將其用作通用類型或方法'RestClient.Execute (RestRequest)'中的參數'T'。你有一個想法,爲什麼我得到這個? – alminh

+0

@alminh正如錯誤信息所解釋的那樣,很可能你正在使用一種抽象的類型。 – Nkosi

+1

感謝您的回覆。我需要在MockRestClient方法的T:new()處添加約束。現在它工作正常。 – alminh