I have some functions I wrote in my project that return true\false and I'm trying to test them with unit test.
public class DbContextDal : DbContext
{
public DbContextDal() : base("BdContextDal_ConnectionString_appConfig3")
{
Configuration.LazyLoadingEnabled = true;
}
public virtual DbSet<User> users { get; set; }
public virtual DbSet<Student> students { get; set; }
public virtual DbSet<Course> courses { get; set; }
public virtual DbSet<Enrollment> Enrollments { get; set; }
}
as you can see DbContextCal is the connecting to data base and I have some functions that returns values. here I'm trying to test it with class I did for unit test
public class Student
{
// variables of this student..
public float getGradeInCourse(Course c)
{
if (s == null || c == null) return -1f;
float grade = -1f;
DbContextDal dal = new DbContextDal();
Enrollment e = dal.Enrollments.Find(s.ID, c.ID);
if (e != null)
grade = e.Grade;
return grade;
}
}
I want to test this function getGradeInCourse and I did unit test for that:
[TestClass()]
public class StudentTests
{
[TestMethod()]
public void getGradeInCourseTest()
{
DbContextDal dal = new DbContextDal();
Student s = dal.students.Find(100);
Course c = dal.courses.Find(900);
Assert.AreEqual(s.getGradeInCourse(c), 76);
}
}
I'm getting this error:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Migrations is enabled for context 'DbContextDal' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.
in this DBcontext, i use almost in each function in my project. I can't use in Mocks because I use in this object Dbcontext many times, for example in the function I tried to test above.