How to fake validation error in MonoRail modular module?

I am running through the castle lock and trying to perform the unit-test action of the controller in which my DTO check is configured. The controller inherits from SmartDispatcherController. The action and DTO look like this:


[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; }
}

unit test is as follows:


[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" is my HomeController instance in the test; home.Errors contains a link to ErrorSummary, which should be placed in Flash if there is a validation error).

I see that the debugger believes that dto does not have a validation error; it clearly needs to have several failures as the test passes.

I read the Joey blog post at this , but it looks like the trunk of the lock has advanced as it was written. Can someone shed some light please?

+3

Source: https://habr.com/ru/post/1698806/


All Articles