// Setup async method when it returns datamockService.Setup(_ =>_.GetAsync(It.IsAny<int>())).ReturnsAsync(It.IsAny<Data>()).Verifiable();// Setup async method when it executes without a return typemockService.Setup(_ =>_.RunAsync()).ReturnsAsync(Task.CompletedTask).Verifiable();// Setup async method when it throws an exceptionmockService.Setup(_ =>_.RunAsync(It.IsAny<int>())).Throws<Exception>().Verifiable();// Verify all service methods, marked as 'Verifiable'mockService.VerifyAll();// Verify individual service methodmockService.Verify(_ =>_.GetAsync(It.IsAny<int>()),Times.Once);mockService.Verify(_ =>_.RunAsync(),Times.Exactly(2));mockService.Verify(_ =>_.RunAsync(It.IsAny<int>()),Times.Never);
Assertions
// NUnitAssert.That(result,Is.InstanceOf<ObjectResult>());varapiResponse=resultasOkObjectResult;Assert.That(apiResponse.StatusCode,Is.EqualTo((int)HttpStatusCode.OK));// xUnitvarapiResponse=Assert.IsType<OkObjectResult>(result);Assert.Equal((int)HttpStatusCode.OK,apiResponse.StatusCode);// FluentAssertionsresult.Should().BeOfType<OkObjectResult>("because we return content").Which.StatusCode.Should().Be((int)HttpStatusCode.OK);// Shouldlyresult.ShouldSatisfyAllConditions(()=>result.ShouldBeOfType<OkObjectResult>(),()=>(resultasOkObjectResult).StatusCode.ShouldBe((int)HttpStatusCode.OK));