How to specify factory Intellitest should be used for the interface?

With Intellitest, you can specify the Intellitest type to use, which is suitable for the interface when creating unit tests, however I have a custom factory that I want to use instead.

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 that PEX is trying to create.

I tried adding the following attribute to PexAssemblyInfo.cs, and I also tried adding it above my test:

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

but I still get this warning at runtime:

will use Company.Logics.SpecificLogic as ILogic

And so it seems that it ignores my factory every time. How to get Intellitest to use my factory instead?

+7
c # moq pex intellitest
source share
1 answer

If you want to use PexCreatableByClassFactory, you need a class that implements the IPexClassFactory interface. 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 specific classes, not with interfaces. Now, if Pex decides that an instance of the Logic class should be created, the following code will be created:

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

If you prefer to use PexFactoryMethod , this is also possible. However, PexFactoryMethod also works with specific classes, for example:

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

If you use both solutions at the same time, i.e. If you define the pex factory method and the pex factory class for the same type, then, in my experience, the pex factory method will have a higher priority.

If you have more than one class that implements the ILogic interface, you need to define a pex factory method and / or a pex factory class for each of these classes. Otherwise, PEX will try to instantiate these classes on its own.

If you want to get rid of this warning, right-click it and select "Fix" from the context menu. Pex will create the following attribute for you:

 [assembly: PexUseType(typeof(SpecificLogic))] 
+2
source share