我想在单元测试中创建继承类 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/