asp.net-mvc – 使用AutoMapper的控制器上的单元测试

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 使用AutoMapper的控制器上的单元测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试单元测试使用AutoMapping的UpdateUser控制器.这是控制器的代码

UpdateUserController

  1. private readonly IUnitOfWork _unitOfWork;
  2. private readonly IWebSecurity _webSecurity;
  3. private readonly IOAuthWebSecurity _oAuthWebSecurity;
  4. private readonly IMapper _mapper;
  5.  
  6. public AccountController()
  7. {
  8. _unitOfWork = new UnitOfWork();
  9. _webSecurity = new WebSecurityWrapper();
  10. _oAuthWebSecurity = new OAuthWebSecurityWrapper();
  11. _mapper = new MapperWrapper();
  12. }
  13.  
  14. public AccountController(IUnitOfWork unitOfWork,IWebSecurity webSecurity,IOAuthWebSecurity oAuthWebSecurity,IMapper mapper)
  15. {
  16. _unitOfWork = unitOfWork;
  17. _webSecurity = webSecurity;
  18. _oAuthWebSecurity = oAuthWebSecurity;
  19. _mapper = mapper;
  20. }
  21.  
  22. //
  23. // Post: /Account/UpdateUser
  24. [HttpPost]
  25. [ValidateAntiForgeryToken]
  26. public ActionResult UpdateUser(UpdateUserModel model)
  27. {
  28. if (ModelState.IsValid)
  29. {
  30. // Attempt to register the user
  31. try
  32. {
  33. var userToUpdate = _unitOfWork.UserRepository.GetByID(_webSecurity.CurrentUserId);
  34. var mappedModel = _mapper.Map(model,userToUpdate);
  35.  
  36. **mappedModel will return null when run in test but fine otherwise (e.g. debug)**
  37.  
  38.  
  39. _unitOfWork.UserRepository.Update(mappedModel);
  40. _unitOfWork.Save();
  41.  
  42. return RedirectToAction("Index","Home");
  43. }
  44. catch (MembershipCreateUserException e)
  45. {
  46. ModelState.AddModelError("",ErrorCodeToString(e.StatusCode));
  47. }
  48. }
  49. return View(model);
  50. }

这是我的单位测试
UpdateUserControllerTest

  1. [Fact]
  2. public void UserRepository_Update_User_Success()
  3. {
  4. Controller = new AccountController(UnitOfWork,WebSecurity.Object,OAuthWebSecurity.Object,Mapper);
  5. const string emailAsUserName = "user@username.com";
  6. const string password = "password";
  7. const string email = "email@email.com";
  8. const string emailNew = "newEmail@email.com";
  9. const string firstName = "first name";
  10. const string firstNameNew = "new first name";
  11. const string lastName = "last name";
  12. const string lastNameNew = "new last name";
  13.  
  14. var updatedUser = new User
  15. {
  16. Email = emailNew,FirstName = firstNameNew,LastName = lastNameNew,UserName = emailAsUserName
  17. };
  18.  
  19. WebSecurity.Setup(
  20. s =>
  21. s.CreateUserAndAccount(emailAsUserName,password,new { FirstName = firstName,LastName = lastName,Email = email },false))
  22. .Returns(emailAsUserName);
  23. updatedUser.UserId = WebSecurity.Object.CurrentUserId;
  24.  
  25. UnitOfWork.UserRepository.Update(updatedUser);
  26. UnitOfWork.Save();
  27.  
  28. var actualUser = UnitOfWork.UserRepository.GetByID(updatedUser.UserId);
  29. Assert.Equal(updatedUser,actualUser);
  30.  
  31. var model = new UpdateUserModel
  32. {
  33. Email = emailAsUserName,ConfirmEmail = emailAsUserName,FirstName = firstName,LastName = lastName
  34. };
  35. var result = Controller.UpdateUser(model) as RedirectToRouteResult;
  36. Assert.NotNull(result);
  37. }

我有一个直觉,当在测试模式下运行时,映射器不会查看我在Global.asax中设置的映射器配置.因为错误只在执行单元测试时发生,而是在运行网站时才发生.我已经创建了一个IMappaer接口作为DI,所以我可以模拟它进行测试.我使用Moq for Mocking和xUnit作为测试框架,我还安装了我还没有使用的AutoMoq.任何想法?谢谢你看我的冗长的职位.希望有人可以帮忙,一直在抓我头上好几个小时,阅读很多帖子.

解决方法

在您的测试中,您需要创建一个嘲笑版本的IMapper界面,否则您不是单元测试,您正在进行集成测试.那么你只需要做一个简单的mockMapper.Setup(m => m.Map(something,somethingElse)).Returns(anotherThing).

如果要在测试中使用真正的AutoMapper实现,那么您需要首先进行设置.您的测试不会自动接收您的Global.asax,您也必须在测试中设置映射.当我进行集成测试时,我通常会在测试夹具设置中调用一个静态AutoMapperConfiguration.Configure()方法.对于NUnit,这是[TestFixtureSetUp]方法,我认为xUnit你只是把它放在构造函数中.

猜你在找的asp.Net相关文章