c# - Nsubstitute: Mocking an object Parameter for Unit Testing -
i new nsubstitute , unit testing. know in unit testing, don't care other dependencies. in order rule applied mock units.
i have example tested code method has object parameter:
class dependency { public int a; public dependency() { // algorithms going on ... = algorithm_output; } } class totest { public int xa; public void foo(dependency dep_input){ xa = dep_input.a; // xa used in algorithm ... } }
i thinking of mocking constructor not figure out how in nsubstitute. ultimately, how test this?
i can not add comment because long, add answer: if want test foo
not need mock ctor dep_input
. example if use moq. can use stub
public interface idependency { int { get; } } public class dependency : idependency { public int { get; private set; } public dependency() { // algorithms going on ... = algorithm_output(); } private static int algorithm_output() { return 42; } } public class totest { public int xa; public void foo(idependency dep_input) { xa = dep_input.a; // xa used in algorithm ... } } [testfixture] public class testclass { [test] public void testwithmoq() { var dependecymock = new mock<idependency>(); dependecymock.setup(d => d.a).returns(23); var totest = new totest(); totest.foo(dependecymock.object); assert.areequal(23, totest.xa); dependecymock.verify(d => d.a, times.once); } [test] public void testwithstub() { var dependecystub = new dependencytest(); var totest = new totest(); totest.foo(dependecystub); assert.areequal(23, totest.xa); } internal class dependencytest : idependency { public int { { return 23; } } } }
Comments
Post a Comment