How to create fake data or mock data in unit test case for method using provider for database cal

priya rani asked on January 9, 2019 13:07

Hi , I am looking for solution to handle database dependency code of method in unit test case

Below is My Registration Controller Class-

public class RegistrationController : Controller
{
 public async Task<ActionResult> Register(RegisterViewModel model)
    {

        // Validate uniqueness of UserName requested
        var uniqueUserNameQuery = UserInfoProvider.GetUsers().WhereEquals("UserName", model.UserName);
        if (uniqueUserNameQuery.Count > 0)
        {
            ModelState.AddModelError("UserName", "UserName is not available!");
        }
     if (!ModelState.IsValid)
        {
            List<StateInfo> stateInfos = GetStateSelection();
            model.State = new SelectList(stateInfos, "StateName", "StateDisplayName");
            return View(model);
        }
}
}

when i call above method in test case it will throw exception at point where i am calling UserInfoProvider.GetUsers because dependency is not handled in test case

so how to create fake data for user provider and how to pass it in method

Recent Answers


Peter Mogilnitski answered on January 9, 2019 15:12

Look at the example on MVC DancingGoat web site.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.