当 Fitnesse 实例化一个夹具时,它会寻找一个默认的公共构造函数。
但是,我想构造函数注入我想在夹具中使用的任何应用程序服务。
即我想写我的夹具这个...
public class MyColumnFixture : ColumnFixture {
private readonly IApplicationService _applicationService;
public ManualExitSetupFixture(IApplicationService applicationService) {
_applicationService = applicationService;
}
public void DoStuff(string arg1) {
_applicationService.DoStuff(arg1);
}
}
到目前为止,我想出的最好的方法是将容器公开为单例(见下文)。但必须有更好的方法。Autofac 与我们使用的许多其他技术完美地集成在一起。
public class AutofacIntegration {
private static IContainer _container;
public static IContainer Container {
get {
if (_container == null) {
var builder = new ContainerBuilder();
builder.RegisterModule<MyApplicationModule>();
_container = builder.Build();
}
return _container;
}
}
}
public class MyColumnFixture : ColumnFixture {
private readonly IApplicationService _applicationService;
public ManualExitSetupFixture() {
_applicationService = AutofacIntegration.Container.Resolve<IApplicationService>();
}
public void DoStuff(string arg1) {
_applicationService.DoStuff(arg1);
}
}
编辑:在迈克的帮助下,在我尝试完成这项工作的过程中包括更多细节......
我创建了一个类,它是从 Fitsharp 反编译的 CreateDefault 的反编译代码中复制粘贴的...
public class CreateDefault<T, P> : Operator<T, P>, CreateOperator<T> where P : class, Processor<T>
{
public bool CanCreate(NameMatcher memberName, Tree<T> parameters)
{
return true;
}
public TypedValue Create(NameMatcher memberName, Tree<T> parameters)
{
...
}
}
...并在 SuiteConfig.xml 中注册...
<suiteConfig>
<ApplicationUnderTest>
<AddAssembly>..\..\Services\Win\MyProj.Fitnesse\MyProj.Fitnesse\bin\Debug\MyProj.Fitnesse.dll</AddAssembly>
</ApplicationUnderTest>
<Fit.Operators>
<Add>MyProj.Fitnesse.CreateDefault`2</Add>
</Fit.Operators>
</suiteConfig>
...这会在尝试加载我的 CreateDefault<,> 类时给出以下异常。
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> fitSharp.Machine.Exception.CreateException: Constructor with 0 parameter(s) failed for type 'MyProj.Fitnesse.CreateDefault`2'. ---> System.ArgumentException: Cannot create an instance of MyProj.Fitnesse.CreateDefault`2[T,P] because Type.ContainsGenericParameters is true.
at System.RuntimeType.CreateInstanceCheckThis()
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at fitSharp.Machine.Engine.RuntimeType.CreateInstance()
at fitSharp.Machine.Engine.CreateDefault`2.CreateWithoutParameters(RuntimeType runtimeType)
--- End of inner exception stack trace ---
at fitSharp.Machine.Engine.CreateDefault`2.CreateWithoutParameters(RuntimeType runtimeType)
at fitSharp.Machine.Engine.CreateDefault`2.Create(NameMatcher memberName, Tree`1 parameters)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at fitSharp.Machine.Engine.MethodMember.TryInvoke(Object[] parameters)
at fitSharp.Machine.Engine.ReflectionMember.Invoke(Object[] parameters)
at fitSharp.Machine.Engine.ProcessorBase`2.Operate[O](Object[] parameters)
at fitSharp.Machine.Engine.Operators`2.Add(String operatorName)
--- End of inner exception stack trace ---
at fitSharp.Machine.Engine.ProcessorExtension.InvokeWithThrow[T](Processor`1 processor, TypedValue instance, MemberName memberName, Tree`1 parameters)
at fitSharp.Machine.Application.SuiteConfiguration.LoadNode(String typeName, XmlNode methodNode)
at fitSharp.Machine.Application.SuiteConfiguration.LoadXml(String configurationXml)
at fitSharp.Machine.Application.ArgumentParser.InvokeArgumentHandler(String switch, String argumentValue)
at fitSharp.Machine.Application.ArgumentParser.Parse(IList`1 commandLineArguments)
at fitSharp.Machine.Application.Shell.Run(IList`1 commandLineArguments)
编辑:非常感谢迈克,现在这是一种享受。
实现非常简单,我相信它可以改进,但这里有一个快速的操作方法......
- 定义自定义 CreateOperator
- 创建 SuiteConfig.xml 以指向 dll 并加载自定义创建运算符
- 启动运行器时指定 SuiteConfig
- 注册灯具
我的 CreateOperator 代码...
public class AutofacCreateOperator : CellOperator, CreateOperator<Cell>
{
private static IContainer _container;
public AutofacCreateOperator()
{
var builder = new ContainerBuilder();
builder.RegisterModule<FitnesseModule>();
_container = builder.Build();
}
public bool CanCreate(NameMatcher memberName, Tree<Cell> parameters)
{
return _container.ComponentRegistry.IsRegistered(new TypedService(Type.GetType(memberName.MatchName)));
}
public TypedValue Create(NameMatcher memberName, Tree<Cell> parameters)
{
return new TypedValue(_container.Resolve(Type.GetType(memberName.MatchName)));
}
}