I need to test that exactly Argument Exception is caugtht. Is it really possible to understand that exception in method is caugtht?
public JsonResult Create(TeamViewModel teamViewModel)
{
JsonResult result = null;
try
{
// here exception throws
var domainTeam = teamViewModel.ToDomain();
...
}
catch (ArgumentException ex)
{
this.ModelState.AddModelError(string.Empty, ex.Message);
result = this.Json(this.ModelState);
}
return result;
}
My Unit test for this method:
public void Create_InvalidTeamAchievements_ArgumentExceptionThrown()
{
Exception exception = null;
string invalidAchievements = CreateInvalidTeamAchievements();
// Arrange
var viewModel = new TeamMvcViewModelBuilder().WithAchievements(invalidAchievements).Build();
var sut = _kernel.Get<TeamsController>();
// Act
try
{
sut.Create(viewModel);
}
catch (ArgumentException ex)
{
exception = ex;
}
// Assert
VerifyExceptionThrown(exception, string.Format(Resources.ValidationTeamAchievements,
Constants.Team.MAX_ACHIEVEMENTS_LENGTH));
}
You are testing it in wrong way. Functionality should be tested to not throw exception as you already have caught exception inside Create Method. Rather you should Assert that JsonResult
containing your ModelState
should have error in it in case exception was raised in Create
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.