【发布时间】:2018-03-17 01:37:11
【问题描述】:
我想在我的单元测试中创建一个继承类 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.
}
}
【问题讨论】:
-
提供一些代码作为minimal reproducible example,以更好地展示您的问题。它可用于帮助识别问题并得出任何可能的解决方案。
-
@Nkosi 我更新了提供最少伪代码的问题。
-
您还必须更详细地说明何时何地出现错误。看看这里的示例,看看你是否能找到一个有帮助的例子github.com/Azure-Samples/service-fabric-watchdog-service/blob/…
-
如果您尝试创建无状态服务的实例,@Nkosi 服务结构会在内部引发 NullReferenceException。描述什么?
标签: unit-testing azure-service-fabric service-fabric-stateless