如何在c#MVC中增加预定义控制器的代码覆盖率

时间:2017-05-23 14:30:18

标签: 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
{
    [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);
        }

    }
}

我写了一个单元测试用例,代码覆盖率达到24%。如果我将控制器代码分成一小段代码&amp;方法,然后调用单元测试类中的那些,覆盖率增加到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);

        }
    }
}

0 个答案:

没有答案