c# - 如何强制 Microsoft Azure .NET API 调用失败?

标签 c# unit-testing azure azure-sdk-.net

我正在构建一个利用 Microsoft Azure 的应用程序,并且我正在使用 Microsoft Azure .NET API 调用来管理我的基础架构。我想进入测试阶段,我很好奇是否有方法可以强制调用失败,其他提供的虚假参数。

例如,我有以下用于启动虚拟机的代码片段。有没有办法让这个调用在给定正确的参数时自动失败?

using (ComputeManagementClient client = new ComputeManagementClient(new CertificateCloudCredentials(c_subscriptionId, MyCert)))
        {
            var status = client.VirtualMachines.Start(serviceName, deploymentName, vmName);
            if (status.Status == OperationStatus.Failed)
            {
                throw new MachineCreationException(status.Error.Code, null);
            }
        }

提前致谢!

最佳答案

根据我的(现已删除)评论,ComputeManagementClient 实现了 IComputeManagementClient。 (有关详细信息,请参阅 MSDN。)一种简单的方法是使用模拟框架来 stub IComputeManagementClient 的实例。 (我使用 Rhino Mocks 。)

有大量资源可用于使用 Rhino Mocks 和其他模拟框架编写单元测试。

关于c# - 如何强制 Microsoft Azure .NET API 调用失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27363947/

相关文章:

c# - 什么时候调用 IEnumerable.GetEnumerator 而不是 IEnumerable<T>.GetEnumerator?

java - Spring Boot 服务层 : Unit or Integration tests?

django - 组织 Selenium 测试和单元测试的最佳实践

java - 部署的 Azure 无服务器函数看不到函数之外的类

c# oracle sql 准备语句

c# - 你能得到C#中哪个子进程调用了构造函数的信息吗?

c# - 反序列化和构造函数的关系

unit-testing - 您如何对不同的类访问级别进行单元测试?

python - sendgrid - 如何指定收件人 SMTP 服务器?

azure - Azure DevOps 中的 Sonar 扫描仪错误 - ERROR : Not authorized. 请检查属性 sonar.login 和 sonar.password