Answer the question
In order to leave comments, you need to log in
Why doesn't validation work when testing a controller?
there is a controller for creating a new user, a model should come into it. I send an empty model through the test and no error comes out. if you add it manually, then everything is triggered by the
controller:
public async Task<ActionResult> Create([FromBody]RegisterViewModel user)
{
// ModelState.AddModelError("swe","ssssss");
if (ModelState.IsValid)
{
var result = await _userService.Create(user);
return Ok(user);
}
else
{
return BadRequest(ModelState);
}
}
public class RegisterViewModel
{
//обязательные поля
[Required(ErrorMessage = "Пустое поле")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Пустое поле")]
public string FullName { get; set; }
[Required(ErrorMessage = "Пустое поле")]
public string UserName { get; set; }
}
public void CheckAddUser()
{
RegisterViewModel fakeUser = new RegisterViewModel();
fakeUser.Password = null;
fakeUser.UserName = null;
fakeUser.Email = null;
var moc = new Mock<IUserService>();
moc.Setup(c => c.Create(It.IsAny<RegisterViewModel>()) );
var userController = new TestController(moc.Object);
var result = userController.Create(fakeUser);
//должен вернуть ошибку
// Assert.IsInstanceOf(result.GetType(),typeof(BadRequestResult));
Assert.AreSame(result,typeof(BadRequestResult));
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question