GoogleCloudVision C# - 如何单元测试服务
我在后端代码中实现了Google Cloud Vision服务。我想单元测试该服务,但我不知道如何嘲笑它。
这是我要嘲笑的当前代码。有没有办法注入ImaNeanNotatorClient,以便我嘲笑它?
public class GoogleCloudVisionService : IGoogleCloudVision
{
private readonly GoogleCloudVisionSettings googleCloudVisionSettings;
private ImageAnnotatorClient client;
public GoogleCloudVisionService(IOptions<GoogleCloudVisionSettings> googleCloudVisionSettings)
{
this.googleCloudVisionSettings = googleCloudVisionSettings.Value.WithParsedKey();
var credential = GoogleCredential.FromJson(this.googleCloudVisionSettings.ApiJson);
ImageAnnotatorClientBuilder clientBuilder = new ImageAnnotatorClientBuilder();
clientBuilder.Credential = credential;
client = clientBuilder.Build();
}
public async Task<SafeSearchAnnotation> GetImageAnnotation(byte[] imageBytes)
{
var image = Image.FromBytes(imageBytes);
var annotation = await client.DetectSafeSearchAsync(image);
return annotation;
}
public async Task<bool> IsImageSafe(byte[] imageBytes)
{
var annotations = await GetImageAnnotation(imageBytes);
var isSafe = annotations.Adult < googleCloudVisionSettings.Annotations.Adult &&
annotations.Racy < googleCloudVisionSettings.Annotations.Racy &&
annotations.Violence < googleCloudVisionSettings.Annotations.Violence &&
annotations.Medical < googleCloudVisionSettings.Annotations.Medical &&
annotations.Spoof < googleCloudVisionSettings.Annotations.Spoof;
return isSafe;
}
}
I've implemented a Google Cloud Vision service in my backend code. I want to unit test the service but I don't know how to mock it.
Here's my current code that I'm going to mock. Is there a way to inject the ImageAnnotatorClient so I could mock it?
public class GoogleCloudVisionService : IGoogleCloudVision
{
private readonly GoogleCloudVisionSettings googleCloudVisionSettings;
private ImageAnnotatorClient client;
public GoogleCloudVisionService(IOptions<GoogleCloudVisionSettings> googleCloudVisionSettings)
{
this.googleCloudVisionSettings = googleCloudVisionSettings.Value.WithParsedKey();
var credential = GoogleCredential.FromJson(this.googleCloudVisionSettings.ApiJson);
ImageAnnotatorClientBuilder clientBuilder = new ImageAnnotatorClientBuilder();
clientBuilder.Credential = credential;
client = clientBuilder.Build();
}
public async Task<SafeSearchAnnotation> GetImageAnnotation(byte[] imageBytes)
{
var image = Image.FromBytes(imageBytes);
var annotation = await client.DetectSafeSearchAsync(image);
return annotation;
}
public async Task<bool> IsImageSafe(byte[] imageBytes)
{
var annotations = await GetImageAnnotation(imageBytes);
var isSafe = annotations.Adult < googleCloudVisionSettings.Annotations.Adult &&
annotations.Racy < googleCloudVisionSettings.Annotations.Racy &&
annotations.Violence < googleCloudVisionSettings.Annotations.Violence &&
annotations.Medical < googleCloudVisionSettings.Annotations.Medical &&
annotations.Spoof < googleCloudVisionSettings.Annotations.Spoof;
return isSafe;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我们的代码库中查找了一些示例以
使其模拟之后,我终于获得了一个工作代码,我需要添加一个新的构造函数,以便我可以通过
mock&lt; ImageAnnotatorClient&gt;
的实例。Finally got a working code after looking some examples in our codebase
To make it mock-able, I need to add a new constructor so I can pass an instance of
Mock<ImageAnnotatorClient>