有关使用Mocks进行单元测试Service Fabric应用程序的特定问题 [英] Specific questions about unit-testing Service Fabric Applications using Mocks
问题描述
此问题是针对以下问题的后续活动:为服务编写单元测试织物应用程序类
This question is a follow up to: Writing UnitTests for a Service Fabric Application Class
我的应用程序定义属于此类-简化了它以解决我面临的最基本问题:
My application definition is of the type - simplified it to solve the most basic of problems I am facing:
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);
}
}
}
}
测试类如下:
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");"中未将其设置为有效值.线.
The exception I get is that "System.NullReferenceException: Object reference not set to an instance of an object." which is arising from the fact that "dataPackage.Path" is not being set to a valid value in the "var dataPackage = Context.CodePackageActivationContext.GetDataPackageObject("Data");" line.
如何使用模拟复制CodePackageActivationContext?所述数据"包括:这里的"DataPackage"是指名称为"Data"的文件夹.并包含SearchServiceClass的代码.
How do I replicate the CodePackageActivationContext using the mock? The "Data" here refers to DataPackage which is a folder by the name "Data" and resides with the code for the SearchServiceClass.
推荐答案
您可以使用下面的代码来模拟该行:
You can use below piece of code to mock the line:
var codePackageActivationContext = new Mock<ICodePackageActivationContext>();
For more information, check this out: How to use Moq framework to unit test azure service fabrics?
对于以下查询:
我还不完全了解的一些东西是变量和在类中创建的成员,但在构造函数之前叫.例如"私有字符串jsonStr;"线似乎得到即使我只是在打电话,也没有在单元测试中大惊小怪地执行单元测试中的构造函数,以及私有字符串jsonStr;"是在构造函数之外.所以同样适用于所有变量在构造函数之外创建的?
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#代码的:在私有字符串jsonStr;
行中,定义了jsonStr.但是,在引用它之前,您应该对其进行初始化,否则它将引发空引用错误-您正在构造函数中执行此操作.
Here, it is about a simple C# code : In the line private string jsonStr;
, jsonStr is defined. But, before referencing it, you should initialize it, otherwise it will throw null reference error - which you are doing in your constructor.
这篇关于有关使用Mocks进行单元测试Service Fabric应用程序的特定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!