Use In-Memory DbContext
Return to menu
Here you use DbContextOptionsBuilder class when creating a new DbContext.
You start out by making a option object to the DbContext
var options = new DbContextOptionsBuilder()
.UseInMemoryDatabase(databaseName: "FpcDb") //The IN-memory database name could be any name you like
.Options;
And create a couple of Employee objects in the DbContext using the options just made.
using (var context = new FPC_Emp_Context(options))
{
context.Add(new Employee
{
ID = 1,
Alias = "Joe Belushi",
Name = "Joe Belushi",
HomeEmail = "belushi@joebelushi.com",
JobStatus = 1,
Remarks = "what a day.....",
DepID = 1,
Mobile = "6600998822",
Valid = true
});
context.Add(new new Employee
{
ID = 2,
Alias = "Arnold Schwartzenegger",
Name = "Arnold Schwartzenegger",
HomeEmail = "as@schwartsen.com",
JobStatus = 1,
Remarks = "I'll be back !",
DepID = 1,
Mobile = "12345678",
Valid = true
});
context.SaveChanges();
}
Here you can call the method which exists in the Controller, here GetEmployeesFromManagement
Finally do some tests.
using (var context = new FPC_Emp_Context(options))
{
HomeController controller = new HomeController(context);
var result = controller.GetEmployeesFromManagement();
Assert.IsNotNull(result, "GetEmployeesFromManagement is NULL");
if (result != null)
{
Assert.IsTrue(result.Count > 0);
Assert.IsInstanceOfType(result, typeof(List));
}
}
Optionally you can test it directly from the Controller returning the ViewResult.
Calling the MyHomeC.Index() method istead of calling GetEmployeesFromManagement method above.
HomeController MyHomeC = new HomeController(ctx);
IActionResult t = MyHomeC.Index();
Assert.IsNotNull(t);
ViewResult result = t as ViewResult;
var cnt = (result.Model as IEnumerable).Count();
Assert.IsTrue(cnt > 0);
Return to menu