为了账号安全,请及时绑定邮箱和手机立即绑定

发出 Moq'ing HttpResponseMessage

发出 Moq'ing HttpResponseMessage

C#
FFIVE 2021-06-29 13:33:17
我有以下方法:public async Task<SecurityRoleDeleteResult> DeleteSecurityRoleByRoleId(int securityRoleId){    string url = $"{_housingDataSecurityConfiguration.HousingDataSecurityWebApiUrl}SecurityRoles/Delete";    HttpResponseMessage message = _apiClientService.Post(url, securityRoleId);    if (message.StatusCode == HttpStatusCode.InternalServerError)    {        return SecurityRoleDeleteResult.ErrorOccurred;    }    int intResult = 0;    var apiResult = await message.Content.ReadAsStringAsync();    if (int.TryParse(apiResult, out intResult))    {        return (SecurityRoleDeleteResult)intResult;    }    else    {        return SecurityRoleDeleteResult.ErrorOccurred;    }}我现在正在尝试为它编写一个单元测试,到目前为止:[Test]public async Task DeleteSecurityRoleByRoleId(){    _mockApiClientService.Setup(a => a.Post(It.IsAny<string>(), It.IsAny<int>()))        .Returns(new HttpResponseMessage {StatusCode = HttpStatusCode.OK});    SecurityRoleDeleteResult result = await _securityRoleService.DeleteSecurityRoleByRoleId(It.IsAny<int>());    Assert.AreEqual(SecurityRoleDeleteResult.Success, result);}这里的问题是,当_securityRoleService.DeleteSecurityRoleByRoleId我在方法中运行测试时,我尝试将var apiResultmessage.content设置为 null,因为在这种情况下,我只是在模拟崩溃。我怎样才能模拟这个以便我的测试能够工作?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信