为 Service Fabric 应用程序编写单元测试 Class
Writing UnitTests for a Service Fabric Application Class
我正在为 Service Fabric 应用程序编写单元测试 class。我 运行 遇到了一些我不知道如何修复的错误。
class 定义是这样的:
namespace SearchService
{
internal sealed class SearchServiceClass : StatelessService
{
//variables defined followed by constructor
private string jsonStr;
public SearchServiceClass(StatelessServiceContext context)
: base(context)
{
//constructor stuff
}
public bool IsDataJsonLoaded
{
get
{
return !(jsonStr == null);
}
}
}
}
应用程序有一个测试 class 定义如下:
namespace SearchService.Tests
{
//[TestClass]
public class SearchServiceClassTest
{
[Fact]
public void SearchServiceClassConstructor()
{
var searchServiceClass = new SearchServiceClass();
Assert.True(searchServiceClass.IsDataJsonLoaded);
}
}
}
我收到以下错误:
There is no argument given that corresponds to the required formal
parameter 'context' of
'SearchServiceClass.SearchServiceClass(StatelessServiceContext)'.
有人可以告诉我如何解决这个问题吗?
编辑:我一直在看 ServiceFabric.Mocks。我的理解是我需要使用 MockStatelessServiceContextFactory.Default
来创建模拟上下文。我该怎么做,下面的方法对吗?:
var searchServiceClass = new SearchServiceClass(MockStatelessServiceContextFactory.Default);
是的,您可以使用 ServiceFabric.Mocks 库,使用以下代码创建服务的测试实例:
var serviceInstance = new SearchServiceClass(MockStatelessServiceContextFactory.Default);
作为 Default
上下文的替代方法,您还可以构建自定义实例:
var newUri = new Uri("fabric:/MockApp/OtherMockStatelessService");
var serviceTypeName = "OtherMockServiceType";
var partitionId = Guid.NewGuid();
var replicaId = long.MaxValue;
var context = new MockCodePackageActivationContext("fabric:/MyApp", "MyAppType", "Code", "Ver", "Context", "Log", "Temp", "Work", "Man", "ManVer");
var context = MockStatelessServiceContextFactory.Create(context, serviceTypeName, newUri, partitionId, replicaId);
var serviceInstance = new SearchServiceClass(context);
有关详细信息,请参阅 this sample test and this one。
我正在为 Service Fabric 应用程序编写单元测试 class。我 运行 遇到了一些我不知道如何修复的错误。 class 定义是这样的:
namespace SearchService
{
internal sealed class SearchServiceClass : StatelessService
{
//variables defined followed by constructor
private string jsonStr;
public SearchServiceClass(StatelessServiceContext context)
: base(context)
{
//constructor stuff
}
public bool IsDataJsonLoaded
{
get
{
return !(jsonStr == null);
}
}
}
}
应用程序有一个测试 class 定义如下:
namespace SearchService.Tests
{
//[TestClass]
public class SearchServiceClassTest
{
[Fact]
public void SearchServiceClassConstructor()
{
var searchServiceClass = new SearchServiceClass();
Assert.True(searchServiceClass.IsDataJsonLoaded);
}
}
}
我收到以下错误:
There is no argument given that corresponds to the required formal parameter 'context' of 'SearchServiceClass.SearchServiceClass(StatelessServiceContext)'.
有人可以告诉我如何解决这个问题吗?
编辑:我一直在看 ServiceFabric.Mocks。我的理解是我需要使用 MockStatelessServiceContextFactory.Default
来创建模拟上下文。我该怎么做,下面的方法对吗?:
var searchServiceClass = new SearchServiceClass(MockStatelessServiceContextFactory.Default);
是的,您可以使用 ServiceFabric.Mocks 库,使用以下代码创建服务的测试实例:
var serviceInstance = new SearchServiceClass(MockStatelessServiceContextFactory.Default);
作为 Default
上下文的替代方法,您还可以构建自定义实例:
var newUri = new Uri("fabric:/MockApp/OtherMockStatelessService");
var serviceTypeName = "OtherMockServiceType";
var partitionId = Guid.NewGuid();
var replicaId = long.MaxValue;
var context = new MockCodePackageActivationContext("fabric:/MyApp", "MyAppType", "Code", "Ver", "Context", "Log", "Temp", "Work", "Man", "ManVer");
var context = MockStatelessServiceContextFactory.Create(context, serviceTypeName, newUri, partitionId, replicaId);
var serviceInstance = new SearchServiceClass(context);
有关详细信息,请参阅 this sample test and this one。