Looking to create anonymous types in AutoFixture for class tests?

I recently started using the AutoFixture library (http://autofixture.codeplex.com/) for Unit Testing, and I really like it.

I got this sample code from the AutoFixture CodePlex website. My question is about line number 8.

1. [TestMethod] 2. public void IntroductoryTest() 3. { 4. // Fixture setup 5. Fixture fixture = new Fixture(); 6. 7. int expectedNumber = fixture.CreateAnonymous<int>(); 8. MyClass sut = fixture.CreateAnonymous<MyClass>(); 9. 10. // Exercise system 11. int result = sut.Echo(expectedNumber); 12. 13. // Verify outcome 14. Assert.AreEqual<int>(expectedNumber, result, "Echo"); 15. // Teardown 16. } 

I cannot understand why we need to create an anonymous object of the tested class.

  MyClass sut = fixture.CreateAnonymous<MyClass>(); 

The class must be a real IMO object. Example.

  var sut = new MyClass(); 

My question is: what is the real benefit of creating an anonymous object for testing?

+6
source share
1 answer

In the trivial case, you are right - there is no material difference.

However, the SOT API Encapsulation takes advantage of its capabilities - as your system test and the Fixture Object become more interesting than anything with the default ctor (does it really have no dependencies?), For example:

  • MyClass requires components to be added to it
  • `MyClass has read / write properties that you do not want to use for default values ​​(coincident programming)
  • the MyClass building has something else that you want to apply the policy to

Then, the start of using Sut Factory is turned on , allowing an outsider to drop off and allow the use of cross-related process.

EDIT: For some reason @ Brad Wilson found it necessary to reprint this article , which is kind of

+3
source

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


All Articles