1

I do not know if my question has any sense (I am new in testing), but I thought that I might want to check the ModelState without calling the controller. Example of my controller:

public ActionResult New(ClientModel client){
    try{
       if (ModelState.IsValid){
         var result = _clienteAppService.Add(client);
         if (result.IsValid){
            return RedirectToAction("Index", new { id = client.ClienteId});
         }
         foreach (var error in result.Erros)
            ModelState.AddModelError(erro.Name, erro.Message);
       }
    }
    catch (Exception e){
       ModelState.AddModelError(string.Empty, e.Message);
    }
    var errors = ModelState.Select(x => x.Value.Errors)
                 .Where(y => y.Count > 0).ToList();
    ViewData["erros"] = errors;
    return View(client);
}

What I mean is that I dont want to happen all of the things that I am doing in the controller, so like in this case add a new client, just check if model that I am sending is correct.

I want to do that because in my case calling the controller is quite time consuming in testing, and I dont want to call it for every scenario.

user0810
  • 886
  • 5
  • 19
  • 33
  • 1
    If your question is "How do I Unit Test my Controller method and ensure validation is happening on ClientModel" [see this question](https://stackoverflow.com/q/22561834) – StuartLC Dec 30 '19 at 15:24
  • 1
    If the operation within the controller is expensive then just do a simple `if (ModelState.IsValid) return...`, that should avoid you having to execute the rest of the controller. But that is a hack and perhaps not what you're looking for. Super quick solution to your stated problem though. – Nico Dec 30 '19 at 15:27

0 Answers0