Autofixture: how to express the following code declaratively?

I am having problems expressing the following code in declarative form:

[Theory] [InlineData(@"-o=C:\Temp\someFile -p=1")] [InlineData(@"-p=1 -o=C:\Temp\someFile")] public void ParseMissingParameterShouldReturnCorrectResult( string argsString ) { ..... var fixture = new Fixture(); fixture.Register<IFoo>(fixture.Create<Foo>); fixture.Register<IBar>(fixture.Create<Bar>); var sut = fixture.Create<SomeClass>(); ..... } 

In my production code, I have something like:

 new SomeClass(new Foo(new Bar)) 

when the SomeClass constructor is defined as:

 public SomeClass(IFoo foo) 

TIA

David

EDIT:

SomeClass looks like

 public class SomeClass : IQux { private readonly IFoo _foo; public SomeClass(IFoo foo) { _foo= foo; } 
+3
source share
1 answer

You can declare SUT (which is a type of SomeClass ) as a parameter of the test method:

 [Theory] [InlineAutoMockData(@"-o=C:\Temp\someFile -p=1")] [InlineAutoMockData(@"-p=1 -o=C:\Temp\someFile")] public void ParseMissingParameterShouldReturnCorrectResult( string argsString, SomeClass sut) { } 

An easy way to create the [InlineAutoMockData] attribute is:

 internal class InlineAutoMockDataAttribute : CompositeDataAttribute { internal InlineAutoMockDataAttribute (params object[] values) : base( new InlineDataAttribute(values), new AutoDataAttribute( new Fixture().Customize( new CompositeCustomization( new AutoMoqCustomization())))) { } } 

Note :

If you also need to set expectations on IFoo or IBar ridiculous instances, you can freeze them so that the same Frozen instances are passed in the SomeClass instance:

 [Theory] [InlineAutoMockData(@"-o=C:\Temp\someFile -p=1")] [InlineAutoMockData(@"-p=1 -o=C:\Temp\someFile")] public void ParseMissingParameterShouldReturnCorrectResult2( string argsString, [Frozen]Mock<IFoo> mock, [Frozen]Mock<IBar> stub, SomeClass sut) { } 
+3
source

Source: https://habr.com/ru/post/945761/


All Articles