【发布时间】:2022-10-13 02:52:58
【问题描述】:
我正在尝试使用 mock 模拟 KeyVaultClient,以便我的单元测试在运行测试时不会依赖于我的 Azure KeyVault 服务。有没有类似于我用来模拟 SecretClient 的代码?
// Create a page of enumeration results
Page<SecretProperties> secretResponsePage = Page<SecretProperties>.FromValues(
new[] {
new SecretProperties("secret1"),
new SecretProperties("secret2"),
new SecretProperties("secret3"),
},
continuationToken: null,
Mock.Of<Response>());
// Create a pageable that consists of a single page
AsyncPageable<SecretProperties> pageable = AsyncPageable<SecretProperties>.FromPages(new[] { secretResponsePage });
// Setup a client mock object to return the pageable when GetPropertiesOfSecretsAsync is called
var clientMock = new Mock<SecretClient>();
clientMock.Setup(c => c.GetPropertiesOfSecretsAsync(It.IsAny<CancellationToken>()))
.Returns(pageable);
【问题讨论】:
-
Moq 只能模拟标记为
virtual的方法或接口。我建议将SecretClient包装在您自己的类中,并从中创建您自己的界面。
标签: c# azure unit-testing nunit