Unit-Testing, on-Controller, der AutoMapper

Ich versuche unit-Tests ein UpdateUser-Controller, der die automatische Zuordnung. Hier ist der code für den controller

UpdateUserController

    private readonly IUnitOfWork _unitOfWork;
    private readonly IWebSecurity _webSecurity;
    private readonly IOAuthWebSecurity _oAuthWebSecurity;
    private readonly IMapper _mapper;

    public AccountController()
    {
        _unitOfWork = new UnitOfWork();
        _webSecurity = new WebSecurityWrapper();
        _oAuthWebSecurity = new OAuthWebSecurityWrapper();
        _mapper = new MapperWrapper();
    }

    public AccountController(IUnitOfWork unitOfWork, IWebSecurity webSecurity, IOAuthWebSecurity oAuthWebSecurity, IMapper mapper)
    {
        _unitOfWork = unitOfWork;
        _webSecurity = webSecurity;
        _oAuthWebSecurity = oAuthWebSecurity;
        _mapper = mapper;
    }

    //
    //Post: /Account/UpdateUser
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UpdateUser(UpdateUserModel model)
    {
        if (ModelState.IsValid)
        {
            //Attempt to register the user
            try
            {
                var userToUpdate = _unitOfWork.UserRepository.GetByID(_webSecurity.CurrentUserId);
                var mappedModel = _mapper.Map(model, userToUpdate);

 **mappedModel will return null when run in test but fine otherwise (e.g. debug)**


                _unitOfWork.UserRepository.Update(mappedModel);
                _unitOfWork.Save();

                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }
        return View(model);
    }

und das ist mein Unit-Test
UpdateUserControllerTest

[Fact]
    public void UserRepository_Update_User_Success()
    {
        Controller = new AccountController(UnitOfWork, WebSecurity.Object, OAuthWebSecurity.Object, Mapper);
        const string emailAsUserName = "[email protected]";
        const string password = "password";
        const string email = "[email protected]";
        const string emailNew = "[email protected]";
        const string firstName = "first name";
        const string firstNameNew = "new first name";
        const string lastName = "last name";
        const string lastNameNew = "new last name";

        var updatedUser = new User
            {
                Email = emailNew,
                FirstName = firstNameNew,
                LastName = lastNameNew,
                UserName = emailAsUserName
            };

        WebSecurity.Setup(
            s =>
            s.CreateUserAndAccount(emailAsUserName, password,
                                   new { FirstName = firstName, LastName = lastName, Email = email }, false))
                   .Returns(emailAsUserName);
        updatedUser.UserId = WebSecurity.Object.CurrentUserId;

        UnitOfWork.UserRepository.Update(updatedUser);
        UnitOfWork.Save();

        var actualUser = UnitOfWork.UserRepository.GetByID(updatedUser.UserId);
        Assert.Equal(updatedUser, actualUser);

        var model = new UpdateUserModel
            {
                Email = emailAsUserName,
                ConfirmEmail = emailAsUserName,
                FirstName = firstName,
                LastName = lastName
            };
        var result = Controller.UpdateUser(model) as RedirectToRouteResult;
        Assert.NotNull(result);
    }

Habe ich ein Bauchgefühl, dass bei Ausführung im Testmodus, der mapper hat sich nicht nur der mapper-Konfiguration, die ich haben-setup im Global.asax. Da der Fehler nur auftreten, während der Ausführung der unit-Tests, aber nicht bei der Ausführung der website, wie Sie ist. Habe ich einen IMappaer Schnittstelle als DI-so kann ich mock es für Testzwecke. Ich verwendet Moq für Spott und xUnit als test-framework habe ich auch installiert AutoMoq, die ich bisher noch nicht benutzt habe. Irgendeine Idee? Danke für das betrachten meiner langen post. Hoffe mir kann jemand helfen, schon meinen Kopf kratzen für Stunden und lese viele Beiträge.

InformationsquelleAutor Steven | 2013-05-08
Schreibe einen Kommentar