我正在尝试编写N个单元测试。单元测试概念的新手。 我有各种必须测试的数据库操作。 在浏览了一些博客之后;我已经建立了一个用于测试的数据库(与项目的数据库相同);
当我编写测试用例并且测试进入控制器时;显然,数据库操作是在实际项目数据库上下文而不是测试数据库上下文上执行的。
使用包装器类被认为是一种解决方案。我读了很多书,但对如何去做一无所知。
此外,我正在通过控制器构造函数使用诸如userManager,signInManager等服务。
我在db操作上不断收到null引用错误,并说UserManager为null的错误; 我意识到我可以简单地创建一个启动类,并将userManager等注入测试项目中。
但是我不知道这是否是一个好习惯。
这就是我实现各种依赖项(SetUp)的方式:
public class HostingClass
{
protected IHostingEnvironment HostingEnvironment { get; private set; }
public HostingClass(IHostingEnvironment host)
{
HostingEnvironment = host;
}
}
[TestFixture]
public class ControllerTest
{
protected TestContext db;
protected ActualDbContext _db;
protected SignInManager<IdentityUser> signInManager;
protected ILogger<LoginModel> logger;
protected RoleManager<IdentityRole> roleManager;
protected UserManager<IdentityUser> userManager;
protected IHostingEnvironment hostingEnvironment;
public ControllerTest()
{
db = new TestContext();
_db = new ActualDbContext ();
}
public IHostingEnvironment hosting()
{
var mockEnvironment = new Mock<IHostingEnvironment>();
mockEnvironment
.Setup(m => m.WebRootPath)
.Returns("path");
return mockEnvironment.Object;
}
public HttpContext GetHttpContext(string name = "test", string[] roles = null)
{
roles = new string[] { "role1" };
var identity = new GenericIdentity(name, string.Empty);
var principal = new GenericPrincipal(identity, roles ?? new string[] { });
var contextUser = principal;
var httpContext = new DefaultHttpContext()
{
User = contextUser
};
return httpContext;
}
要测试的Db操作的控制器动作:
public async Task<IActionResult> AddToDb(ViewModel addToDb)
{
IdentityUser user = new IdentityUser { UserName = addToDb.Email, Email = addToDb.Email };
string password = addToDb.FirstName.ToLower() + "P@ssword12";
db.Add(addToDb);
db.SaveChanges();
----
----
----
var result = await _userManager.CreateAsync(user, password);
try
{
if (result.Succeeded)
{
if (!await _roleManager.RoleExistsAsync("Role1"))
{
IdentityRole userRole = new IdentityRole("Role1");
IdentityResult createRole = await _roleManager.CreateAsync(userRole);
if (createRole.Succeeded)
{
await _userManager.AddToRoleAsync(user, "Role1");
}
}
else
{
await _userManager.AddToRoleAsync(user, "Role1");
}
}
测试用例:
public async Task Verify_AddToDb_Successful_True()
{
var databaseData= db.TableA.Select(x => x).FirstOrDefault();
hostingEnvironment = hosting();
Controller controller = new Controller(hostingEnvironment, signInManager, logger, roleManager, userManager);
var actionResult = await controller.AddToDb(databaseData) as RedirectToActionResult;
Assert.AreEqual("ViewName", actionResult.ActionName);
}
角色测试:
将用户添加到角色:
[TestCase]
public async Task Verify_RoleManagement_AddUserToRole_True()
{
var user = new IdentityUser();
user.Email = "roleadditiontest.com";
var userRole = "Role2";
Controller controller = new Controller(hostingEnvironment, signInManager, logger, roleManager, userManager);
var actionResult = adminController.AddUserToRole(user, userRole).Result;
Assert.IsTrue(actionResult);
}
AddUserToRole基本上只使用用户和角色,并使用usermanager将用户添加到角色中。
为了测试上述操作,我使用了基于几篇文章的测试Db。我无法遵循存储库模式
我也不知道从哪里开始。我正在使用NUnit 3;我想知道以下内容: