Pregunta

Estoy ejecutando en el baúl de Castle, y tratando de realizar una prueba unitaria de una acción del controlador donde se configura la validación de mi DTO. El controlador hereda de SmartDispatcherController. La acción y el DTO parecen:


[AccessibleThrough(Verb.Post)]
public void Register([DataBind(KeyReg, Validate = true)] UserRegisterDto dto)
{
    CancelView();
    if (HasValidationError(dto))
    {
        Flash[KeyReg] = dto;
        Errors = GetErrorSummary(dto);
        RedirectToAction(KeyIndex);
    }
    else
    {
        var user = new User { Email = dto.Email };
        // TODO: Need to associate User with an Owning Account
        membership.AddUser(user, dto.Password);
        RedirectToAction(KeyIndex);
    }
}

public class UserRegisterDto
{
    [ValidateNonEmpty]
    [ValidateLength(1, 100)]
    [ValidateEmail]
    public string Email { get; set; }

    [ValidateSameAs("Email")]
    public string EmailConfirm { get; set; }

    [ValidateNonEmpty]
    public string Password { get; set; }

    [ValidateSameAs("Password")]
    public string PasswordConfirm { get; set; }

    // TODO: validate is not empty Guid
    [ValidateNonEmpty]
    public string OwningAccountIdString { get; set; }

    public Guid OwningAccountId
    {
        get { return new Guid(OwningAccountIdString); }
    }

    [ValidateLength(0, 40)]
    public string FirstName { get; set; }

    [ValidateLength(0, 60)]
    public string LastName { get; set; }
}

La prueba de la unidad se parece a:


[Fact]
public void Register_ShouldPreventInValidRequest()
{
    PrepareController(home, ThorController.KeyPublic, ThorController.KeyHome, HomeController.KeyRegister);

    var dto = new UserRegisterDto { Email = "ff" };
    home.Register(dto);

    Assert.True(Response.WasRedirected);
    Assert.Contains("/public/home/index", Response.RedirectedTo);
    Assert.NotNull(home.Errors);
}

(" home " es mi instancia de HomeController en la prueba; home.Errors contiene una referencia a un ErrorSummary que se debe colocar en Flash cuando se produce un error de validación).

Estoy viendo que el depurador piensa que dto no tiene un error de validación; claramente debería tener varios fallos, la forma en que se ejecuta la prueba.

He leído la publicación del blog de Joey sobre esto , pero parece que el tronco del Castillo se ha movido desde que se escribió esto. ¿Alguien puede arrojar algo de luz, por favor?

¿Fue útil?
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top