Definition / Control> 1 constructor parameters of the same type with independent values ​​when using AutoFixture as SutFactory

Using AutoFixture as a SutFactory , I see that if I register or freeze a value for a type, this value will then be used for all subsequent applications of this type. However, if I have a class with two parameters that have the same type in the constructor, for example:

public class ClassA { public double ParameterA { get; private set;} public double ParameterB { get; private set;} public ClassA(double parameterA, double parameterB) { ParameterA = parameterA; ParameterB = parameterB; } public void Execute(ClassB object) { object.Value = (object.Value * ParameterA) /ParameterB; } } 

What strategies exist for using autofixture to enter unique predefined values ​​for parameter A and parameter B in order to test the Estimated value?

* Unfortunately, I can’t share my exact script here, however its using the command template to work with another object, therefore the only way to set parameter A and parameter B that supports the design is to enter them both in and the constructor is the easiest way do it in this case.

+8
c # tdd autofixture
source share
3 answers

Using the functions a bit more, I personally found that there are not many objects where this happens, so creating a simple setup for a class is probably best here, for example:

 public class ClassACustomization: ICustomization { public double ParameterA { get; set;} public double ParameterB { get; set;} public void Customize(IFixture fixture) { //Note: these can be initialized how you like, shown here simply for convenience. ParameterA = fixture.Create<double>(); ParameterB = fixture.Create<double>(); fixture.Customize<ClassA>(c => c .FromFactory(() => new ClassA(ParameterA , ParameterB ))); } } 

I find that this still gives me great flexibility in AutoFixture, without compromising the ability to inject ParameterA or ParameterB in certain cases (you can configure them using constructor methods, constructor, etc.). Instead tuning to random values).

+2
source share

One option is to configure the creation algorithm for all instances of ClassA .

 var valueA = 1; var valueB = 2; var fixture = new Fixture(); fixture.Customize<ClassA>(c => c .FromFactory(() => new ClassA(valueA, valueB))); var result = fixture.Create<ClassA>(); // -> parameterA value is 1 // -> parameterB value is 2 
+5
source share

It is my observation that in most cases when I come across a similar scenario, I do not need to control the values ​​before they are passed to the constructor, but rather I need to know what the values ​​are.

This is easy to do by adding Structural Control Properties to the class:

 public class ClassA { public ClassA(double parameterA, double parameterB) { this.A = parameterA; this.B = parameterB; } public double A { get; private set } public double B { get; private set } } 

Now you can ask AutoFixture to instantiate the ClassA class without further use, and then query the instance for the values.

+5
source share

All Articles