简体   繁体   English

如何指定Intellitest应该用于接口的工厂?

[英]How do I specify the factory Intellitest should use for an interface?

With Intellitest you can specify a type for Intellitest to use that fits an interface when generating unit tests, however I have a custom factory I wish to use instead. 使用Intellitest,您可以在生成单元测试时为Intellitest指定一个适合接口的类型,但是我有一个我希望使用的自定义工厂。

My custom factory: 我的定制工厂:

public static partial class LogicFactory
{
    /// <summary>A factory for ILogic instances</summary>
    [PexFactoryMethod(typeof(ILogic))]
    public static ILogic Create(string defaultUICulture, bool saveSuccessful)
    {
        return Mock.Of<ILogic>(
        x =>
        x.GetUICulture(It.IsAny<string>()) == defaultUICulture &&
        x.Save(It.IsAny<string>(), It.IsAny<string>()) == saveSuccessful);
    }
}

I would like to use this factory for all ILogic instances PEX tries to create. 我想将这个工厂用于PEX尝试创建的所有ILogic实例。

I tried adding the following attribute to PexAssemblyInfo.cs, and I also tried adding it above my test: 我尝试将以下属性添加到PexAssemblyInfo.cs,我也尝试将其添加到我的测试之上:

[assembly: PexCreatableByClassFactory(typeof(ILogic), typeof(LogicFactory))]

but I still get this runtime warning when instrumenting code: 但在检测代码时我仍然会收到此运行时警告:

will use Company.Logics.SpecificLogic as ILogic

And so it seems it's ignoring my factory every time. 所以似乎每次都忽略了我的工厂。 How can I force Intellitest to use my factory instead? 我怎样才能强迫Intellitest使用我的工厂呢?

If you want to use PexCreatableByClassFactory you need a class that implements IPexClassFactory interface. 如果要使用PexCreatableByClassFactory ,则需要一个实现IPexClassFactory接口的类。 Here is an example: 这是一个例子:

public partial class LogicFactory : IPexClassFactory<Logic>
{
    public Logic Create()
    {
        //...
    }
}

[assembly: PexCreatableByClassFactory(typeof(Logic), typeof(LogicFactory))]

It should be noted that IPexClassFactory works with concrete classes and not with interfaces. 应该注意的是, IPexClassFactory使用具体类而不是接口。 Now if Pex decides that an instance of Logic class should be created, the following code will be generated: 现在,如果Pex决定应该创建Logic类的实例,那么将生成以下代码:

LogicFactory2 s2 = new LogicFactory();
Logic s1 = ((IPexClassFactory<Logic>)s2).Create();

If you prefer to use PexFactoryMethod it is also possible. 如果您更喜欢使用PexFactoryMethod ,也可以使用。 However, PexFactoryMethod also works with concrete classes eg: 但是, PexFactoryMethod也适用于具体类,例如:

 [PexFactoryMethod(typeof(Logic))]
 public static Logic Create(string defaultUICulture, bool saveSuccessful)
 {
      //...
 }

If you use both solutions at the same time ie define a pex factory method and a pex factory class for the same type, then according to my experience a pex factory method will have a higher priority. 如果您同时使用两种解决方案,即为同一类型定义pex工厂方法和pex工厂类,那么根据我的经验,pex工厂方法将具有更高的优先级。

If you have more than one class that implements ILogic interface you need to defined a pex factory method and/or a pex factory class for each of these classes. 如果您有多个实现ILogic接口的类,则需要为每个类定义一个pex工厂方法和/或一个pex工厂类。 Otherwise PEX will try to create instances of these classes on his own. 否则,PEX将尝试自己创建这些类的实例。

If you want to get rid of mentioned warning right click it and select Fix from a context menu. 如果要删除上述警告,请右键单击它并从上下文菜单中选择“ 修复” Pex will generate the following attribute for you: Pex将为您生成以下属性:

[assembly: PexUseType(typeof(SpecificLogic))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM