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?
Spock source share