有关使用 Mock 对 Service Fabric 应用程序进行单元测试的具体问题
Specific questions about unit-testing Service Fabric Applications using Mocks
此问题是以下问题的跟进:
我的应用程序定义属于以下类型 - 简化它以解决我面临的最基本的问题:
namespace SearchService
{
internal sealed class SearchServiceClass : StatelessService
{
//variables defined followed by constructor
private string jsonStr;
public SearchServiceClass(StatelessServiceContext context)
: base(context)
{
try
{
var dataPackage = Context.CodePackageActivationContext
.GetDataPackageObject("Data");
jsonStr = File.ReadAllText(dataPackage.Path + @"\data.json");
}
catch
{
//exception handling code
throw;
}
}
public bool IsDataJsonLoaded
{
get
{
return !(jsonStr == null);
}
}
}
}
测试 class 看起来像:
namespace SearchService.Tests
{
[TestClass]
public class SearchServiceClassTest
{
[TestMethod]
public void SearchServiceClassConstructor()
{
var searchServiceClass = new SearchServiceClass(MockStatelessServiceContextFactory.Default);
Assert.IsTrue(searchServiceClass.IsDataJsonLoaded);
}
}
}
我得到的异常是 "System.NullReferenceException: 对象引用未设置到对象的实例。" 这是由于 "dataPackage.Path" 未在“var dataPackage = Context.CodePackageActivationContext.GetDataPackageObject("Data"); 中设置为有效值行。
如何使用模拟复制 CodePackageActivationContext?这里的“Data”指的是DataPackage,它是一个名为“Data”的文件夹,里面存放着SearchServiceClass的代码。
您可以使用以下代码来模拟该行:
var codePackageActivationContext = new Mock<ICodePackageActivationContext>();
有关更多信息,请查看:
对于以下查询:
Some thing else I don't completely understand, are variables and
members being created in the class, but before the constructor is
called. For e.g. "private string jsonStr;" line does seem to get
executed without fuss in the Unit Test, even though I am only calling
the constructor in the Unit Test, and the "private string jsonStr;" is
outside the constructor. So will the same apply to all the variables
created outside the constructor?
这里是一段简单的C#代码:在private string jsonStr;
行定义了jsonStr。但是,在引用它之前,你应该初始化它,否则它会抛出空引用错误——这是你在构造函数中做的。
此问题是以下问题的跟进:
我的应用程序定义属于以下类型 - 简化它以解决我面临的最基本的问题:
namespace SearchService
{
internal sealed class SearchServiceClass : StatelessService
{
//variables defined followed by constructor
private string jsonStr;
public SearchServiceClass(StatelessServiceContext context)
: base(context)
{
try
{
var dataPackage = Context.CodePackageActivationContext
.GetDataPackageObject("Data");
jsonStr = File.ReadAllText(dataPackage.Path + @"\data.json");
}
catch
{
//exception handling code
throw;
}
}
public bool IsDataJsonLoaded
{
get
{
return !(jsonStr == null);
}
}
}
}
测试 class 看起来像:
namespace SearchService.Tests
{
[TestClass]
public class SearchServiceClassTest
{
[TestMethod]
public void SearchServiceClassConstructor()
{
var searchServiceClass = new SearchServiceClass(MockStatelessServiceContextFactory.Default);
Assert.IsTrue(searchServiceClass.IsDataJsonLoaded);
}
}
}
我得到的异常是 "System.NullReferenceException: 对象引用未设置到对象的实例。" 这是由于 "dataPackage.Path" 未在“var dataPackage = Context.CodePackageActivationContext.GetDataPackageObject("Data"); 中设置为有效值行。
如何使用模拟复制 CodePackageActivationContext?这里的“Data”指的是DataPackage,它是一个名为“Data”的文件夹,里面存放着SearchServiceClass的代码。
您可以使用以下代码来模拟该行:
var codePackageActivationContext = new Mock<ICodePackageActivationContext>();
有关更多信息,请查看:
对于以下查询:
Some thing else I don't completely understand, are variables and members being created in the class, but before the constructor is called. For e.g. "private string jsonStr;" line does seem to get executed without fuss in the Unit Test, even though I am only calling the constructor in the Unit Test, and the "private string jsonStr;" is outside the constructor. So will the same apply to all the variables created outside the constructor?
这里是一段简单的C#代码:在private string jsonStr;
行定义了jsonStr。但是,在引用它之前,你应该初始化它,否则它会抛出空引用错误——这是你在构造函数中做的。