I am trying to unit test a controller which takes in a dbcontext and a usermanager object. How could I go about passing these into the controller when performing unit testing?
Controller
public ProjectsController(ApplicationDbContext context,
UserManager<ApplicationUser> userManager)
{
_context = context;
_userManager = userManager;
}
ApplicationDbContext
public ApplicationDbContext(
DbContextOptions options,
IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
{
}
public DbSet<Project> Projects { get; set; }
Test
[Fact]
public void test4()
{
//....
var controller = new ProjectsController(null, null);
var result = controller.GetProject(p.ProjectId);
}
Controller method being tested
[HttpGet("{id}")]
public async Task<ActionResult<Project>> GetProject(Guid id)
{
var project = await _context.Projects.FindAsync(id); // Find project by id
var query = _context.Users.Where(u => u.Projects.Any(p => p.ProjectId.ToString().Equals(project.ProjectId.ToString()))); //find all users for the project
project.AssignedUsersToProject = query.ToList(); //set found users to the assigned users list
if (project == null)
{
return NotFound();
}
return project;
}