I just had a problem with setting up a Unit Test using xUnit
and Moq
.
My Testee
The Method TestMethod()
contains the Logic, I want to test. It is async and though returns a Task
public class Testee
{
private readonly IDoSomething _doSomething;
public Testee(IDoSomething doSomething)
{
_doSomething = doSomething;
}
public async Task TestMethod()
{
await _doSomething.DoAsync(true); // throws NullReferenceException in Unit-Test
}
}
The Interface IDoSomething
looks like this
public interface IDoSomething
{
Task DoAsync(bool aboolean);
}
The Unit-Test
Now im Setting up my Unit-Test like this.
public class TesteeTest
{
[Fact]
public async Task MyTest()
{
var mock = new Mock<IDoSomething>();
mock.Setup(x => x.DoAsync(It.IsAny<bool>())).Verifiable();
var testee = new Testee(mock.Object);
await testee.TestMethod(); // Test breaks here because it throws an exception
mock.Verify(x => x.DoAsync(true), Times.Once);
mock.Verify(x => x.DoAsync(false), Times.Never);
}
}
Why am I getting this exception?