1

我想在我的单元测试中创建一个继承类 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.
    }
}
4

1 回答 1

3

可以办到。但与其重新发明轮子,不如看看服务结构模拟https://github.com/loekd/ServiceFabric.Mocks

它包含适合您的场景类型的有用助手。

于 2017-10-07T07:02:27.847 回答