mardi 31 octobre 2017

Should I Use It.Is

So I'm a bit new to this testing business, and using the Moq library. I'm wondering about the use of the It.Is() method.

Say I have a class I'd like to put under test:

public class ZeroChecker
{
    public bool IsNotZero(int myInt)
    {
        return myInt != 0;
    }
}

So I make a corresponding class to test:

public class ZeroCheckerTest
{
    [Fact]
    public void IsNotZero_ReturnsTrue_WhenInputIsNotZero()
    {
        //Arrange
        var myInt = It.Is<int>(i => i != 0);
        ZeroChecker target = new ZeroChecker();

        //Act
        bool actual = target.IsNotZero(myInt);

        //Assert
        Assert.True(actual);
    }
}

However, my test fails! When I look in the debugger, I notice that myInt is set to zero!

So I'm considering that either:

1) I'm dumb, and this is not how one should use It.Is()

2) There's a bug in Moq

And in either case, how would I go about testing the above scenario? Switch to [InlineData()] and throw in a handful of non-zero ints, I suppose?

Aucun commentaire:

Enregistrer un commentaire