i've been using autofixture / automoqdata / nunit several months now, quite happily far.
but i'm getting stuck on weird:
if want run single test in test suite (using testdriven), seems automoqdata attribute launched several times (between 5 , 9, don't know why, , decides how many times runs).
the problem if try freeze object instance in autofixture customizations, it'll end freezing 5 9 objects, using many fixture instances, , have no clue 1 injected in test.
is there way avoid ? why happen in first place ?
sample code :
public class automoqdataattribute : autodataattribute { public automoqdataattribute() : base(new fixture() .customize(new automoqcustomization()) .customize(new frozenfoocustomization()) // freezing object .customize(new frozenfixturecustomization()) // freezing passed fixture have fixture instance available in test body .customize(new staticbindingcustomization()) // binding frozen object static reference ) { } } public class frozenfoocustomization : icustomization { public void customize(ifixture fixture) { fixture.freeze<foo>(); } } public class foo { } public class frozenfixturecustomization : icustomization { public void customize(ifixture fixture) { fixture.inject(fixture); } } public class staticbindingcustomization : icustomization { public void customize(ifixture fixture) { fixture.freeze<mock<isomeutilities>>(); var someutilities = fixture.create<mock<isomeutilities>>(); someutilities.setup(x => x.getstuff()).returns(fixture.create<foo>()); // executed several times, each time different instance of fixture, returning different instance of foo someextensions.factory = x => someutilities.object; // last "someutilities.object" bound factory, test relies on frozen version generated 1 of previous fixture instances } } [testfixture] public class test { [test, automoqdata] public void sometest( [frozen]mock<isomeutilities> objmock, someobjecttotest sut) { assert.istrue(referenceequals(objmock.object.getstuff(), sut.getstuff())); } } public class someobjecttotest { } public static class someextensions { public static func<object, isomeutilities> factory { get; set; } = x => new someutilities(x); public static object getstuff(this object obj) { return factory(obj).getstuff(); } } public class someutilities : isomeutilities { private readonly object _obj; public someutilities(object obj) { _obj = obj; } public object getstuff() { return _obj; // whatever } } public interface isomeutilities { object getstuff(); }
edit: someextensions.factory factory used make extensions mockable, based on daniel cazzulino's approach.
i'm not big fan of extensions , hope refactor them @ point, time. question here why there several fixture instances, making frozen someutilities mock used in factory different 1 used in test, , test fail.
edit2: following @markseemann's advice, reported issue on autofixture repo.
Comments
Post a Comment