When unit testing, how do I mock a return null from async method?

前端 未结 3 1161
面向向阳花
面向向阳花 2021-02-18 20:45

Normally, I mock my repo like so:

var repository = new Mock();
repository.Setup(r => r.GetMemberAsync(email))
    .Returns(Task.FromResult         


        
3条回答
  •  醉酒成梦
    2021-02-18 21:17

    Old question but you can also do this which I think it cleaner:

    Assuming the default value of your object is null you can also use:

    default()
    

    e.g.

    default(Member)
    default(List)
    etc.
    

    Full Example:

    var myRepo = new Mock();
    myRepo 
        .Setup(p => p.GetAsync("name"))
        .ReturnsAsync(default(List));
    

提交回复
热议问题