c# - Can't grasp the difference between Freeze/Inject/Register -


before starting, i'm big fan of autofixture, i'm still in curve of learning how use tool. having developed autofixture mr ploeh , contributors.

so let's start question.

according autofixture/automoq ignores injected instance/frozen mock

the interesting part of above link given code

mock<isettings> settingsmock = new mock<isettings>(); settingsmock.setup(s => s.get(settingkey)).returns(xmlstring);  isettings settings = settingsmock.object; fixture.inject(settings); 

to mark answer can rewritten

fixture.freeze<mock<isettings>>()        .setup(s => s.get(settingkey)).returns(xmlstring); 

it looks syntaxic sugar, using freeze method way write in fluent interface creation of mock, configuration, , injection in autofixture container.

after doing research on web, there're functional difference between freeze , inject. found question: https://github.com/autofixture/autofixture/issues/59 point answer how can freeze null instance in autofixture

the author of link above describe freeze method following:

internally, freeze creates instance of requested type (e.g. ipaypalconfiguration) , injects return instance when request again

i understand when do

var customer = fixture.freeze<order>(); 

it use same instance of order whenever our code request order type. if specify in freeze constructor want use specific instance ?

here's little code example:

[fact] public void methodename() {     var fixture = new fixture().customize(new automoqcustomization());     fixture.freeze<orderline>(new orderline("foo"));     var order = fixture.create<order>(); }  public class order {     private readonly orderline _line;      public order(orderline line)     {         _line = line;     } } public class orderline {     private readonly string _name;      public orderline(string name)     {         _name = name;     } } 

shouldn't name of orderline equal "foo" instead of namefe48163a-d5a0-49a5-b349-7b11ba5f804b ? documentation of freeze method say:

<typeparam name="t">the type freeze.</typeparam> <param name="fixture">the fixture.</param> <param name="seed">any data adds additional information when creating anonymous object. hypothetically, value might value being frozen, not likely.</param> 

why author not sure when value returned ? if specify, instance in constructor of freeze, i'm expecting autofixture use instance ?

then

please notice isn't used frozen value, unless you've customized this. if wish inject specific value fixture, should use method instead.`

it seems have customize seed parameter. can clarify ? solution pointed documentation use inject method. , indeed, works in code example orderline.

i'm looking understand difference between freeze, inject, , register which, according source code, called inject method takes lambda.

register , inject

once upon time, there no inject , no freeze; register ruled code.

back then, there register overload defined thusly:

public static void register<t>(this ifixture fixture, t item) 

however, had share api close relative:

public static void register<t>(this ifixture fixture, func<t> creator) 

the creator of autofixture thought good, alas: users stricken confusion. grievously, user write:

fixture.register(() => universe.lightup()); 

but also

fixture.register(universe.lightup); 

which means exact same thing, because universe.lightup reference method, , matches delegate.

however, syntax looks property reference, if lightup had been property instead of method, first overload selected compiler.

this caused confusion, register<t>(this ifixture fixture, t item) overload renamed inject<t>(this ifixture fixture, t item).

freeze

freeze has different history. long time ago, when still used autofixture in imperative way, noticed repeatedly wrote code this:

var foo = fixture.create<foo>(); fixture.inject(foo); 

so decided concept , named freeze. freeze method shorthand 2 lines of code.

i'm looking understand difference between freeze, inject, , register which, according source code, called inject method takes lambda

in general, shouldn't hard distinguish between inject , register, since signatures don't collide. thus, if try accomplish goal 1 of 2 methods, , code compiles, chose right version.

this case freeze if wasn't overload used in op:

[editorbrowsable(editorbrowsablestate.never)] public static t freeze<t>(this ifixture fixture, t seed) 

notice overload has editorbrowsablestate.never, because confuses people. however, despite that, apparently people still find overload, think it should moved in autofixture 4. it's 1 of features exist because easy implement...


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -