unit testing - c# - Assert Expressions -
i have 'example' class , create unit test it. take on classes below:
public class example { private readonly calculator _calculator; public example(icalculator calculator) { _calculator = calculator; } public void calculate() { _calculator.execute(operation => operation.subtract()); } } public interface ioperation { void sum(); void subtract(); } public inferface icalculator { void execute(action<ioperation> action); } public class calculator { public void execute(action<ioperation> action){} }
what want create unit test class verify method example class calculate
calls _calculator.execute
passing parameter operation.subtract()
. possible?
i know how mock icalculator , verify execute
being called once, have no idea how validade if execute
method called using operation.subtract()
parameter instead of operation.sum()
.
i using nunit
create unit tests. here can see how unit test class @ moment:
[testfixture] public class exampletests { [test] public void test1() { var calculator = new mock<icalculator>(); var subject = new example(calculator.object); subject.calculate(); calculator.verify(x => x.execute(it.isany<action<ioperation>>()), times.once); } }
hope can understand english, sorry that.
you cannot directly verify lambda passed can go around actually invoking said lambda yet mock:
var calculator = new mock<icalculator>(); var operation = new mock<ioperation>(); // when calculator's execute called, invoke it's argument (action<ioperation>) // mocked ioperation later verified calculator .setup(c => c.execute(it.isany<action<ioperation>>())) .callback<action<ioperation>>(args => args(operation.object)); var example = new example(calculator.object); example.calculate(); calculator.verify(c => c.execute(it.isany<action<ioperation>>())); operation.verify(o => o.subtract());
Comments
Post a Comment