unit-testing - 如何在服务结构中对无状态服务进行单元测试

标签 unit-testing azure-service-fabric service-fabric-stateless

我想在单元测试中创建继承类 StatelessService 的类 A 的实例。但我不能。我已经尝试了一切:模拟依赖关系、实现我自己的上下文等等。

当我尝试创建实例时,StatelessService 在内部某处抛出 NullReferenceException。

能做到吗?

class A : StatelessService
{
    public A(StatelessServiceContext context) : base(context /* Here will be thrown NullReferenceException */)
    {
        // It will never even get there.
    }
}

class UnitTest
{
    public void TestMethod()
    {
        var activationContext = MOCK<ICodePackageActivationContext>();
        var context = new StatelessServiceContext(..., activationContext, ...);
        var a = new A(context); // Here will be thrown an exception.
    }
}

最佳答案

这是可以做到的。但不要重新发明轮子,而是看看服务结构模拟 https://github.com/loekd/ServiceFabric.Mocks

它包含适合您的场景类型的有用帮助程序。

关于unit-testing - 如何在服务结构中对无状态服务进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46585534/

相关文章:

node.js - 如何用 sinon 模拟事件?

java - 使用 Mockito 对流畅的界面进行单元测试

c# - Service Fabric 多实例问题

azure - 在 Service Fabric 应用程序中动态创建服务

javascript - Mocha : comparing two objects, 排除像 createdAt 这样的字段

r - 如何为返回数据帧的函数编写单元测试

azure - ServiceFabric SDK 安装失败

azure-service-fabric - 如何访问 Azure Service Fabric 有状态/无状态服务中的 settings.xml?

azure - 如何下载Service Fabric证书?

service-fabric-stateless - 如何重新启动 Service Fabric 应用程序