Can you fool objects to return two desired results?

Is it possible to mock objects to return more than one desired result, as shown below?

mockObject.Setup(o => o.foo(It.IsAny<List<string>>())).Returns(fooBall);
mockObject.Setup(o => o.foo(It.IsAny<int>())).Returns(fooSquare);
+4
source share
2 answers

Yes, you can use these settings. Thus, the arguments to the method call are foodifferent (any integer and any list of strings), there are two different settings, each of which has its own return value. If you had the same arguments, the last installation replaced the previous settings.

Remember - each setting is determined by the member you invoke and the arguments you pass.

+3

, foo , , , , , Moq.

, , It.Is<> , , . , . o.foo(int):

mockObject.Setup(o => o.foo(It.IsAny<int>())).Returns(defaultFoo);
mockObject.Setup(o => o.foo(It.Is<int>(i => i == 5))).Returns(fooBall);
mockObject.Setup(o => o.foo(It.Is<int>(i => i > 50 && i % 2 == 0)))
   .Returns(fooSquare);
+1

Source: https://habr.com/ru/post/1544379/


All Articles