i using nunit , autofac's moq setup test
[testfixture] public class sometests { [onetimesetup] public void initialize() { } [test] public void salarycheck() { using (var mock = automock.getloose()) { // arrange mock.mock<icommonservices>().setup(x => x.addtwonumbers(1,2)).returns(5); var sut = mock.create<somemanager>(); // act int actul = sut.calculatesalary(1); var expected = 5; // assert assert.areequal(expected, actul); } } }
calculatesalary function looks this
public int calculatesalary(int hours) { var addres = _commonservice.addtwonumbers(5,3); if (addres == 5) { return addres * hours; } else { return 100; } }
i want addtwonumbers function external dependency, return 5 no matter what. thats why setting after mocking it. when debug test looks goes inside calsulate salary function returns "0" addtwonumbers function. kind of default or null value.
it not return me 5 i.e. set return.
you need use it.isany<int>()
i.e.
mock.mock<icommonservices>().setup(x => x.addtwonumbers(it.isany<int>(),it.isany<int>())).returns(5);
Comments
Post a Comment