如何在服务结构中单元测试无状态服务
问题描述:
我想创建一个类A的实例,它在我的单元测试中继承了StatelessService类。但我不能。我尝试了一切:嘲笑依赖关系,实现我自己的上下文等。如何在服务结构中单元测试无状态服务
当我尝试创建一个实例时,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.
}
}
提供一些代码作为[mcve],可以更好地展示您的问题。它可以用来帮助识别问题并推导出任何可能的解决方案。 – Nkosi
@Nkosi我更新了提供最小伪代码的问题。 – EwanCoder
对于何时何地出现错误,您还需要更具描述性。看看这里的示例,看看你是否可以找到一个示例,帮助https://github.com/Azure-Samples/service-fabric-watchdog-service/blob/90c6418a99b5fcd777a5b7c2a5f6443fdcc9f42a/TestStatelessService/TestStatelessService.cs – Nkosi