我有一个帐户控制器,我想在模态引导程序中输入错误的用户名/密码时它会返回一条消息"无效登录尝试".从ActionResult登录()到模态.
我的_loginPartialView:
<div id="loginModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Login</h4>
</div>
<div class="modal-body">
<section id="loginForm">
@using (Ajax.BeginForm("Login", "Account", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "loginModal" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" …Run Code Online (Sandbox Code Playgroud) 我正在学习并尝试使用单元测试来完成我的项目.但是当我尝试使用单元测试编写演示时,我看到控制器的单元测试与服务层相同.下面是我为控制器和服务层编写的单元测试代码
控制器测试:
private Mock<ICountryService> _countryServiceMock;
CountryController objController;
List<Country> listCountry;
[TestInitialize]
public void Initialize()
{
_countryServiceMock = new Mock<ICountryService>();
objController = new CountryController(_countryServiceMock.Object);
listCountry = new List<Country>() {
new Country() { Id = 1, Name = "US" },
new Country() { Id = 2, Name = "India" },
new Country() { Id = 3, Name = "Russia" }
};
}
[TestMethod]
public void Country_Get_All()
{
//Arrange
_countryServiceMock.Setup(x => x.GetAll()).Returns(listCountry);
//Act
var result = ((objController.Index() as ViewResult).Model) as List<Country>;
//Assert
Assert.AreEqual(result.Count, 3); …Run Code Online (Sandbox Code Playgroud) 我正在尝试为控制器编写单元测试以测试方法返回所有用户.但我很困惑如何用automapper编写单元测试
控制器:
private readonly IUserService _userService;
public UserController(IUserService userService)
{
this._userService = userService;
}
public ActionResult List()
{
var users = _userService.GetAllUsers().ToList();
var viewModel = Mapper.Map<List<UserViewModel>>(users);
return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)
控制器测试:
private Mock<IUserService> _userServiceMock;
UserController objUserController;
List<UserViewModel> listUser;
[SetUp]
public void Initialize()
{
_userServiceMock = new Mock<IUserService>();
objUserController = new UserController(_userServiceMock.Object);
listUser = new List<UserViewModel>()
{
new UserViewModel() {Id = 1, Active = true, Password = "123456", UserName = "hercules"},
new UserViewModel() {Id = 2, Active = false, Password = "1234567", …Run Code Online (Sandbox Code Playgroud)