我目前正在尝试使用Speflow设置新的.NET Core项目,并将默认的Microsoft Dependency Injection连接到它。
研究了文档和Autofac / Ninject实现之后,我尝试实现自己的运行时插件“ SpecFlow.MSDependencyInjection.SpecFlowPlugin”。
现在,我正在努力解决Specflow特定的基础架构,例如ScenarioContext。
public static class DependencyInjectionExtension
{
private static void RegisterSpecflowDependecies(IObjectContainer objectContainer, ServiceCollection serviceCollection)
{
serviceCollection.AddSingleton(objectContainer);
serviceCollection.AddSingleton((x) =>
{
var specflowContainer = x.GetRequiredService<IObjectContainer>();
var scenarioContext = specflowContainer.Resolve<ScenarioContext>();
return scenarioContext;
});
serviceCollection.AddSingleton((x) =>
{
var specflowContainer = x.GetRequiredService<IObjectContainer>();
var scenarioContext = specflowContainer.Resolve<FeatureContext>();
return scenarioContext;
});
serviceCollection.AddSingleton((x) =>
{
var specflowContainer = x.GetRequiredService<IObjectContainer>();
var scenarioContext = specflowContainer.Resolve<TestThreadContext>();
return scenarioContext;
});
}
[ScenarioDependencies]
public static IServiceProvider CreateServiceCollection(IObjectContainer objectContainer)
{
var serviceCollection = new ServiceCollection();
RegisterSpecflowDependecies(objectContainer, serviceCollection);
var bindingAttributeClasses = typeof(DependencyInjectionExtension).Assembly.GetTypes()
.Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))).ToArray();
foreach (var bindingAttributeClass in bindingAttributeClasses)
{
serviceCollection.AddSingleton(bindingAttributeClass);
}
return serviceCollection.BuildServiceProvider();
}
}
当Specflow尝试解析我的绑定之一时,出现以下异常:
BoDi.ObjectContainerException: 'Primitive types or structs cannot be resolved: System.String (resolution path: TechTalk.SpecFlow.ScenarioContext->TechTalk.SpecFlow.ScenarioInfo)'
RegisterSpecflowDependecies的代码与Autofac实现非常相似。所以我不明白为什么它在我的实现中不起作用。
答案 0 :(得分:0)
似乎其中一个构造函数具有一个字符串参数。
您可以将该构造函数包装在另一个以另一种方式获取参数的类中:
class MyWrapper : ClassThatHasAStringParameter
{
public MyWrapper()
: base("PARAMETER_VALUE")
{
}
}