Reputation: 701
I have a settings class where I have an interface with the following generic method:
T GetValue<T>(string key, bool optional = false, T defaultValue = default(T));
I am now working on another part that uses this setting system and is using the above method to extract information from the setting object:
Machine.BaudRate = Settings.GetValue<int>("BAUDRATE");
Machine.ComPort = Settings.GetValue<int>("COMPORT");
I would like this to be tested by a unit test so I write the following:
Settings.Setup(s => s.GetValue<int>("BAUDRATE",It.IsAny<bool>(),It.IsAny<int>())).Returns(57600);
Settings.Setup(s => s.GetValue("COMPORT", It.IsAny<bool>(),It.IsAny<int>())).Returns(5);
But when I run the test it returns 0 on both of them. If I put in a callback I can see that callback is hit, so the setup has worked correctly. Why won't it return the values indicated in the setup?
Callback example (I have a exception variable I just set to null to have something to break on during debug):
Settings.Setup(s => s.GetValue("COMPORT", false,It.IsAny<int>())).Callback(() => ex = null).Returns(5);
Upvotes: 0
Views: 207
Reputation: 701
I don't know why in my code I get 0 returned, but I found a way to verify my inputs. My inputs are routed through the class i am testing and out to another interface. Becuase of this I can just change setups on that facade to validate the input:
Facade.SetupSet(f => f.BaudRate = It.IsAny<int>()).Callback((int value) => Assert.AreEqual(57600, value)).Verifiable();
Facade.SetupSet(f => f.ComPort = It.IsAny<int>()).Callback((int value) => Assert.AreEqual(5, value)).Verifiable();
Upvotes: 1