1

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.

2
  • 1
    I think you wan to look into Inversion of Control and Dependency Injection. Or do you really want your test to be hitting an actual database? (This seems unlikely).The stuff you are testing (e.g. GetGradeInCourse()) shouldn't be dependant on hitting an actual database. Commented Jun 8, 2017 at 14:10
  • Yes, i want to hit my actual database because I didn't plan right my project that it can make that.. Commented Jun 8, 2017 at 15:00

1 Answer 1

1

You can mock the DB with a framework that allows it, and instead of using your real database just modify the behavior of the mocked one to make your test pass. i'm using Typemock isolator for this, for example:

[TestMethod,Isolated]
public void TestMethod1()
{
    var fakeDBContextDal = Isolate.Fake.NextInstance<DbContextDal>();
    var s = new Student();
    var c = new Course();
    s.ID = 1;
    c.ID = 2;

    var e = new Enrollment();
    e.Grade = 100;

    Isolate.WhenCalled(() => fakeDBContextDal.Enrollments.Find(1,2)).WithExactArguments().WillReturn(e);

    Assert.AreEqual(s.getGradeInCourse(c), 100);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.