单元测试.Net核心Visual Studio 2017中的控制器中的所有操作方法

问题描述 投票:1回答:1
 public async Task<IActionResult> Create(DonorViewModel be, IFormFile pic)
        {
            be.RegCampId = Convert.ToInt32(TempData["Camp"]);
            if (ModelState.IsValid)
            {
                DONOR entity = new DONOR(); 

                #region Insert Entities
                entity.Address = be.Address;
                entity.BarCode = be.BarCode;
                entity.BloodGroupId = be.BloodGroupId;
                entity.CityId = be.CityId;
                entity.CNIC = be.CNIC;
                entity.DOB = be.DOB;
                entity.Email = be.Email;
                entity.EmergencyContact = be.EmergencyContact;
                entity.FullName = be.FullName;
                entity.GenderId = be.GenderId;
                entity.HomeNo = be.HomeNo;
                entity.IsActive = true;
                entity.IsDeleted = false;
                entity.LastDonDate = be.LastDonDate;
                entity.MaritalStatus = be.MaritalStatus;
                entity.MobileNo = be.MobileNo;
                entity.Occupation = be.Occupation;
                entity.PreDonCount = be.PreDonCount;
                if (be.RegCampId != 0) { entity.RegCampId = be.RegCampId; entity.RegistrationTypeId = 3; }
                if (be.RegLocId != 0) { entity.RegLocId = be.RegLocId; entity.RegistrationTypeId = 2; }
                entity.SignPic = entity.SignPic;
                entity.WhatsApp = be.WhatsApp;
                entity.CreatedBy = (int)HttpContext.Session.GetInt32("UserId");
                entity.CreatedDateTime = DateTime.Now;

                #endregion
                flag = await _donorContext.AddAsync(entity);

                if (pic == null || pic.Length <= 0)
                    be.Pic = Path.Combine(_hostingEnvironment.WebRootPath, "images", "Avatar.png").Replace(_hostingEnvironment.WebRootPath, "").Replace("\\", "/");

                if (pic != null && pic.Length > 0)
                {
                    var path = Path.Combine(new string[]
                    {
                            _hostingEnvironment.WebRootPath,
                            "Reservoir","Donor",entity.Id.ToString(),
                            entity.Id + Path.GetExtension(pic.FileName)
                    });
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                    using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        pic.CopyTo(stream);
                    }
                    path = path.Replace(_hostingEnvironment.WebRootPath, "").Replace("\\", "/");
                    entity.Pic = path;

                    entity.CreatedBy = entity.CreatedBy;
                    entity.CreatedDateTime = entity.CreatedDateTime;
                    entity.IsActive = true;
                    entity.IsDeleted = false;
                    await _donorContext.UpdateAsync(entity);
                }

                if (flag)
                {
                    TempData["Message"] = "Donor is Added Successfully.";
                    if (be.RegCampId != 0)
                    {
                        return RedirectToAction("Create", "Donor", new { CampId = be.RegCampId });
                    }
                    else
                    {
                        return RedirectToAction("Create", "Donor");
                    }
                }
            }
            ViewData["RegCampId"] = new SelectList(_context.BLOOD_CAMP, "Id", "City", be.RegCampId);
            ViewData["BloodGroupId"] = new SelectList(_bloodGroupContext.GetAll(), "Id", "Value", be.BloodGroupId);
            ViewData["CityId"] = new SelectList(_cityContext.GetAll(), "Id", "Name", be.CityId);
            ViewData["ScreenedBy"] = new SelectList(_context.EMPLOYEE, "Id", "FirstName", be.ScreenedBy);
            ViewData["GenderId"] = new SelectList(_genderContext.GetAll(), "Id", "Name", be.GenderId);
            ViewData["RegLocId"] = new SelectList(_locationService.GetAll(), "Id", "Name",be.RegLocId);

            return View(be);
        }


This is My Create method In Controller How to unit test it using UnitTest.

using HMS_Presentation.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;


//Unit Test code .

namespace HMS_UnitTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {

            DonorController Controller = new DonorController();

            ViewResult result = Controller.Create() as ViewResult;

            Assert.AreEqual("",????);


        }
    }
}

这是我的单元测试类代码如何使用我的控制器对象来检查操作并测试它。我应该在断言中写什么?我在互联网上搜索它,但没有找到任何适当的解决方案,请检查下面的代码并告诉我应该在断言中写什么。我正在使用Visual Studio 2017和.NET CORE 2.0并在我的解决方案中添加了单元测试项目。

我跟着的链接。 https://docs.microsoft.com/en-us/visualstudio/test/getting-started-with-unit-testing?view=vs-2017

unit-testing asp.net-core
1个回答
0
投票

在ASPNET Core 2.1中引入了一种称为MVC应用程序的功能测试的功能。使用TestServer帮助简化MVC应用程序的内存端到端测试。见下面的例子

using Xunit;

namespace TestingMvc.Tests
{
    public class TestingMvcFunctionalTests : IClassFixture<WebApplicationTestFixture<Startup>>
    {
        public TestingMvcFunctionalTests(WebApplicationTestFixture<Startup> fixture)
        {
            Client = fixture.CreateClient();
        }

        public HttpClient Client { get; }

        [Fact]
        public async Task GetHomePage()
        {
            // Arrange & Act
            var response = await Client.GetAsync("/");

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
    }
}

阅读更多关于MVC应用程序click here的功能测试

© www.soinside.com 2019 - 2024. All rights reserved.