Asp.net mvc 如何提高c#MVC中预定义控制器的代码覆盖率

Asp.net mvc 如何提高c#MVC中预定义控制器的代码覆盖率,asp.net-mvc,unit-testing,code-coverage,vs-unit-testing-framework,Asp.net Mvc,Unit Testing,Code Coverage,Vs Unit Testing Framework,我必须编写单元测试用例,并增加预写控制器的代码覆盖率 using System.Collections.Generic; using Newtonsoft.Json; using System; using System.Web.Mvc; using TW.Data.Business; using TW.Data.Service; using TW.Web.Models; using TW.Common.Utility; namespace TW.Web.Controllers { [

我必须编写单元测试用例,并增加预写控制器的代码覆盖率

using System.Collections.Generic;
using Newtonsoft.Json;
using System;
using System.Web.Mvc;
using TW.Data.Business;
using TW.Data.Service;
using TW.Web.Models;
using TW.Common.Utility;

namespace TW.Web.Controllers
{
    [Authorize]
    public class BillinInfoSearchController : Controller
    {
        private IBillinInfoSearch _IRepository;
        private IResponseMessage _ServiceResponse;

        public BillinInfoSearchController(IBillinInfoSearch irepository, IResponseMessage serviceresponse)
        {
            _IRepository = irepository;
            _ServiceResponse = serviceresponse;
        }

        public ActionResult ActionMethod()
        {
            BillinInfoSearchViewModel viewModel = new BillinInfoSearchViewModel();
            viewModel.Success = false;

            try
            {
                if (!Sitecore.Context.PageMode.IsExperienceEditor)
                {
                    //Getting Custom Properties
                    var userBillAccountNumber = Sitecore.Context.User.Profile.GetCustomProperty(UserProfileDetails.BillAccountNumber);
                    var userObjectIdentifier = Sitecore.Context.User.Profile.GetCustomProperty(UserProfileDetails.ObjectIdentifier);

                    if (!string.IsNullOrEmpty(userBillAccountNumber) && !string.IsNullOrEmpty(userObjectIdentifier))
                    {
                        viewModel.ModelList = new List<BillinInfoSearchModel>();
                        _ServiceResponse = _IRepository.SubmitModelDetails(userBillAccountNumber, userObjectIdentifier);

                        if (_ServiceResponse.Success && !string.IsNullOrEmpty(_ServiceResponse.Content) && _ServiceResponse.Content.Contains("Top_GUID")
                            && !Sitecore.Context.PageMode.IsExperienceEditorEditing)
                        {
                            List<BillinInfoSearchModel> objectList = JsonConvert.DeserializeObject<List<BillinInfoSearchModel>>(_ServiceResponse.Content);

                            if (objectList.Count > 0)
                            {
                                viewModel.Success = true;
                                viewModel.ModelList = objectList;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Error(ex.Message, this);
            }
            return View("~/Views/OAM/BillinInfoSearchView.cshtml", viewModel);
        }

    }
}
使用System.Collections.Generic;
使用Newtonsoft.Json;
使用制度;
使用System.Web.Mvc;
使用TW.Data.Business;
使用TW.Data.Service;
使用TW.Web.Models;
使用TW.Common.Utility;
命名空间TW.Web.Controllers
{
[授权]
公共类BillinInfoSearchController:控制器
{
私人信息搜索;
私人IResponseMessage\u服务响应;
公共账单信息搜索控制器(IBillinInfoSearch irepository、IResponseMessage serviceresponse)
{
_IRepository=IRepository;
_ServiceResponse=ServiceResponse;
}
公共操作结果操作方法()
{
BillinInfoSearchViewModel viewModel=新的BillinInfoSearchViewModel();
viewModel.Success=false;
尝试
{
如果(!Sitecore.Context.PageMode.IsExperienceEditor)
{
//获取自定义属性
var userBillAccountNumber=Sitecore.Context.User.Profile.GetCustomProperty(UserProfileDetails.BillAccountNumber);
var userObjectIdentifier=Sitecore.Context.User.Profile.GetCustomProperty(UserProfileDetails.ObjectIdentifier);
如果(!string.IsNullOrEmpty(userBillAccountNumber)和&!string.IsNullOrEmpty(userObjectIdentifier))
{
viewModel.ModelList=新列表();
_ServiceResponse=\u IRepository.SubmitModelDetails(userBillAccountNumber,userObjectIdentifier);
if(_ServiceResponse.Success&&!string.IsNullOrEmpty(_ServiceResponse.Content)&&&&&u ServiceResponse.Content.Contains(“Top\u GUID”)
&&!Sitecore.Context.PageMode.IsExperienceEditorEdit)
{
List objectList=JsonConvert.DeserializeObject(\u ServiceResponse.Content);
如果(objectList.Count>0)
{
viewModel.Success=true;
viewModel.ModelList=对象列表;
}
}
}
}
}
捕获(例外情况除外)
{
Sitecore.Diagnostics.Log.Error(例如,此消息);
}
返回视图(“~/Views/OAM/BillinInfoSearchView.cshtml”,viewModel);
}
}
}
我编写了一个单元测试用例,代码覆盖率达到24%。如果我将控制器代码分成一小段代码和方法,然后在单元测试类中调用这些代码和方法,覆盖率将增加到65%,但代码部署在UAT中,不想更新部署的代码

我们是否可以在不修改现有控制器的情况下增加代码覆盖率

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;
using System.Web.Mvc;
using TW.Data.Business;
using TW.Data.Service;
using TW.Web.Controllers;
using TW.Common.Utility;
namespace TW.UnitTest.Controller
{
    [TestClass]
    public class BillinInfoSearchControllerTest
    {
        [TestMethod]
        public void BillinInfoSearchControllerViewName()
        {
            IBillinInfoSearch _IRepository = Substitute.For<IBillinInfoSearch>();
            IResponseMessage _ServiceResponse = Substitute.For<IResponseMessage>();

            BillinInfoSearchController twLiveController = new BillinInfoSearchController(_IRepository, _ServiceResponse);

            Sitecore.Context.User.Profile.SetCustomProperty(UserProfileDetails.BillAccountNumber, "102134");
            Sitecore.Context.User.Profile.SetCustomProperty(UserProfileDetails.ObjectIdentifier, "ef84bc1c-825c-4d8c-9801-3c3fc511a40e");

            ViewResult action = twLiveController.ActionMethod() as ViewResult;
            Assert.AreEqual("~/Views/OAM/BillinInfoSearchView.cshtml", action.ViewName);

        }
    }
}
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用替代品;
使用System.Web.Mvc;
使用TW.Data.Business;
使用TW.Data.Service;
使用TW.Web.Controllers;
使用TW.Common.Utility;
命名空间TW.UnitTest.Controller
{
[测试类]
公共类BillinInformationSearchControllerTest
{
[测试方法]
public void BillinInfoSearchControllerViewName()
{
IBillinInfoSearch_IRepository=Substitute.For();
IResponseMessage_ServiceResponse=替换为();
BillinInfoSearchController twLiveController=新的BillinInfoSearchController(\u IRepository,\u ServiceResponse);
Sitecore.Context.User.Profile.SetCustomProperty(UserProfileDetails.BillAccountNumber,“102134”);
Sitecore.Context.User.Profile.SetCustomProperty(UserProfileDetails.ObjectIdentifier,“ef84bc1c-825c-4d8c-9801-3c3fc511a40e”);
ViewResult action=twLiveController.ActionMethod()作为ViewResult;
Assert.AreEqual(“~/Views/OAM/BillinInfoSearchView.cshtml”,action.ViewName);
}
}
}

如果是我,我会重构它以增加可测试性。现在,有很多东西是你无法模仿或伪造的。
我们可以在不修改现有控制器的情况下增加代码覆盖率吗?
简短回答:诺曼人需要将一个大方法分解为一个小方法,然后只有我们可以增加计数。