I am not completely new to unit testing, however I am new to the Moq library and I have run into a problem. I am confused on why my unit test fails. Here is the unit test I am trying to write.
[TestMethod]
public void TestFunction()
{
// Arrange
var options = new Dictionary<string, string>() { { keyValue, true.ToString() } };
//optionsMock.Setup(a => a.ContainsKey(It.Is<string>(b => b == keyValue))).Returns(false);
//optionsMock.Setup(c => c.Add(It.Is<string>(d => d == keyValue), It.Is<string>(e => e == true.ToString()))).Verifiable();
//optionsMock.Setup(f => f[It.Is<string>(g => g == keyValue)]).Returns(true.ToString());
// Act
int projectId = sut.Open(stringValue, booleanValue, stringValue, stringValue, IDictionary<string, string>, out errorString);
// Assert
//optionsMock.Verify(a => a.ContainsKey(It.Is<string>(b => b == keyValue)), Times.Exactly(2));
//optionsMock.Verify(c => c.Add(It.Is<string>(d => d == keyValue), It.Is<string>(e => e == true.ToString())), Times.Once());
//optionsMock.Verify(f => f[It.Is<string>(g => g == keyValue)], Times.Once());
Assert.AreNotEqual(0, id);
}
The problem I am experiencing is that eventually sut.Open(...) makes a call to an internal class that has an IDictionary as a parameter. In that method there is a check to see if the dictionary is null. Whenever I run the unit test without a real dictionary, the null check always evaluates to true and I get a false fail. However if the real dictionary is passed in, I get a passing test. Here is an example of the internal code.
public int Open(..., IDictionary<string, string>, ...)
{
//...
InternalClass.Method(IDictionary<string, string>);
//...
}
Here is the internal classes method
public void Open(..., IDictionary<string, string> dictionary, ...)
{
if(dictionary != null)
{
//... Do something
}
else
{
//... Do something else
}
}
It always executes Do something else, is there away around this?
Aucun commentaire:
Enregistrer un commentaire