Handling specimen creation inconsistencies between AutoFixture and Moq -


i using automoqcustomization in test conventions.

consider code below. works great until add constructor 1 of concrete classes. when do, "could not find parameterless constructor". know autofixture doesn't have issue constructor because delivered me test object one proved assignable ithings... no failure there. must moq.

this makes sense because assume builder generated moq , passed getcommands method. think can see control has been passed autofixture moq @ point.

that takes care of why, should it? there way instruct moq on how deal thingone or there way instruct autofixture ignore moq ithingbuilders , instead fixtury?

public class testclass {     public interface ithingbuilders     {         t1 build<t1>() t1 : ithings;     }     public interface ithings     {     }     public class thingone : ithings     {         public thingone(string someparam)         {         }     }     public class thingtwo : ithings     {     }     public class someclass     {         public list<ithings> getcommands(ithingbuilders builder)         {             var newlist = new list<ithings>();             newlist.add(builder.build<thingone>());             newlist.add(builder.build<thingtwo>());             return newlist;         }     }     [theory, basicconventions]     public void whycannotinstantiateproxyofclass(thingone one, thingtwo two, ithingbuilders builder, someclass sut)     {         assert.isassignablefrom<ithings>(one);         assert.isassignablefrom<ithings>(two);          var actual = sut.getcommands(builder);          assert.equal(1, actual.oftype<thingone>().count());         assert.equal(1, actual.oftype<thingtwo>().count());     } } 

as there's no extensibility point in moq enables autofixture hook in , supply value of thingone, there's not whole lot can do.

however, can use setreturnsdefault<t> method of moq. modifying above test this:

[theory, basicconventions] public void whycannotinstantiateproxyofclass(     thingone one, thingtwo two, ithingbuilders builder, someclass sut) {     assert.isassignablefrom<ithings>(one);     assert.isassignablefrom<ithings>(two);     mock.get(builder).setreturnsdefault(one); // add make test pass      var actual = sut.getcommands(builder);      assert.equal(1, actual.oftype<thingone>().count());     assert.equal(1, actual.oftype<thingtwo>().count()); } 

this bit easier having write specific setup/returns pair, not much. move code autofixture customization, again, since generic method on a mock instance, you'll explicitly need call e.g. thingone in order set default return type. not particularly flexible.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -