如何使 json 序列化的 httpclient 模拟方法通用
How to make httpclient mock method generic for json serialization
我有以下私有方法允许我模拟并因此测试 HttpClient
private Mock<HttpClient> GetMockHttClient(HttpStatusCode desiredStatusCode, User user)
{
var httpMessageHandler = new Mock<HttpMessageHandler>();
httpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync((HttpRequestMessage request, CancellationToken token) =>
{
var response = new HttpResponseMessage
{
Content = new StringContent(JsonSerializer.Serialize(user)), //<-- this could be anything
ReasonPhrase = null,
RequestMessage = null,
StatusCode = desiredStatusCode,
Version = new Version(1, 0)
};
return response;
});
var httpClientMock = new Mock<HttpClient>(httpMessageHandler.Object)
{
Object =
{
BaseAddress = new Uri("[Redacted]")
}
};
return httpClientMock;
}
现在我正在传递一个 User 对象,但是我还有其他测试,例如我可能想要 int[]
序列化或 List<User>
序列化。
复制并粘贴此方法并更改我希望序列化的对象似乎很荒谬,那么如何使此方法通用,以便在调用它时指定将序列化为的类型json.
private Mock<HttpClient> GetMockHttClient<TModel>(HttpStatusCode desiredStatusCode, TModel model)
{
var httpMessageHandler = new Mock<HttpMessageHandler>();
httpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync((HttpRequestMessage request, CancellationToken token) =>
{
var response = new HttpResponseMessage
{
Content = new StringContent(JsonSerializer.Serialize<TModel>(model)),
ReasonPhrase = null,
RequestMessage = null,
StatusCode = desiredStatusCode,
Version = new Version(1, 0)
};
return response;
});
var httpClientMock = new Mock<HttpClient>(httpMessageHandler.Object)
{
Object =
{
BaseAddress = new Uri("[Redacted]")
}
};
return httpClientMock;
}
我有以下私有方法允许我模拟并因此测试 HttpClient
private Mock<HttpClient> GetMockHttClient(HttpStatusCode desiredStatusCode, User user)
{
var httpMessageHandler = new Mock<HttpMessageHandler>();
httpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync((HttpRequestMessage request, CancellationToken token) =>
{
var response = new HttpResponseMessage
{
Content = new StringContent(JsonSerializer.Serialize(user)), //<-- this could be anything
ReasonPhrase = null,
RequestMessage = null,
StatusCode = desiredStatusCode,
Version = new Version(1, 0)
};
return response;
});
var httpClientMock = new Mock<HttpClient>(httpMessageHandler.Object)
{
Object =
{
BaseAddress = new Uri("[Redacted]")
}
};
return httpClientMock;
}
现在我正在传递一个 User 对象,但是我还有其他测试,例如我可能想要 int[]
序列化或 List<User>
序列化。
复制并粘贴此方法并更改我希望序列化的对象似乎很荒谬,那么如何使此方法通用,以便在调用它时指定将序列化为的类型json.
private Mock<HttpClient> GetMockHttClient<TModel>(HttpStatusCode desiredStatusCode, TModel model)
{
var httpMessageHandler = new Mock<HttpMessageHandler>();
httpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync((HttpRequestMessage request, CancellationToken token) =>
{
var response = new HttpResponseMessage
{
Content = new StringContent(JsonSerializer.Serialize<TModel>(model)),
ReasonPhrase = null,
RequestMessage = null,
StatusCode = desiredStatusCode,
Version = new Version(1, 0)
};
return response;
});
var httpClientMock = new Mock<HttpClient>(httpMessageHandler.Object)
{
Object =
{
BaseAddress = new Uri("[Redacted]")
}
};
return httpClientMock;
}