publicvirtual async Task<string> SomeQuery(){// virtual is mandatory in order to mock it

return"someResult";

}

}

publicclass Sut(){// system under test

Service srv;

public Sut(Service srv){

this.srv = srv;

}

public async Task SomeAction(){

var result = await srv.SomeQuery(); // NullReferenceException in here

....

}

}

The problem is that Moq will make the stubbed method return null when invoked, because we are not specifying any return value. The default value for Task<string> is null. We should tell Moq to return a proper Task: