unit-testing - 在 .Net core visual studio 2017 中对控制器中的所有操作方法进行单元测试
问题描述
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);
}
这是我在控制器中的 Create 方法 如何使用 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
解决方案
在 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 应用程序功能测试的更多信息,请单击此处
推荐阅读
- php - 英国邮政编码 - 按距离排序
- node.js - async.parallel 如何在单线程的 nodejs 中工作
- objective-c - 使用 kinfo_proc 时出现 EXC_BAD_ACCESS 错误
- objective-c - Objective-C,EXC_BAD_ACCESS
- javascript - 如何从anguar2中的请求正文中获取数据
- css - 让我的盒子在 css 中响应
- java - 为什么我的 SOCKS5 UDP 实现不起作用?
- angular - 我可以使用一些自定义颜色更改 Angular Material 复选框的颜色吗?如何更改?
- javascript - 元素像亚马逊帮助页面一样保持悬停
- python - Python,在windows中获取用户名,然后保存到文本文件中