Call same method twice with different parameters

How to do this below? Object under test calls ReadAppSetting twice to get two different app settings and defaults a value when it cannot be found. First call returns null (it means the first moq setup did not work), second time ReadAppSetting returns as expected (moq setup worked).

var mock = new Mock<IConfigReader>();
mock.Setup(foo => foo.ReadAppSetting("PropA", "George"))
    .Returns("George");
mock.Setup(foo => foo.ReadAppSetting("PropB", "5000"))
    .Returns("5000");
MyClass objectUnderTest = new MyClass(mock.Object);
...

If mock object invocation returns null then you haven't provide exactly same arguments values as used on mock setup. Check how you are invoking config reader in MyClass . Possibly you are passing wrong key value, or wrong default value.

BTW if you want config reader to return default values for all invocations, then you can use single setup:

mock.Setup(foo => foo.ReadAppSetting(It.IsAny<string>(), It.IsAny<string>()))
    .Returns<string, string>((key, defaultValue) => defaultValue);
链接地址: http://www.djcxy.com/p/35340.html

上一篇: C#

下一篇: 用不同的参数调用两次相同的方法