where_am_I
where_am_I

Reputation: 11

How do I setup an integration test for a CRUD app for ASP.NET MVC 5 using Entity Framework 6?

I having a functional CRUD app that (simply put) creates, reads, updates, and deletes teachers from a localdb through a GUI.

Regarding create, I have two action results located in the controller:

public ActionResult Create()
{
    return View();
}

[HttpPost]
public ActionResult Create(ModelTeacher oTeacher)
{
    if (!ModelState.IsValid)
    {
        // Return to the view with the validation error messages
        return View(oTeacher);
    }

    db.DbTeachers.Add(oTeacher);
    db.SaveChanges();

    return RedirectToAction("SuccessCreate", "Teacher");
}

I haven't found resources that click with me on how to test CRUD. I prefer to do an integration test and go through the motion of CRUD so the final test (delete) will remove it from the database. I feel a mock database is not necessary because it is such a small localdb.

My idea to test this controller is to recreate a controller in a test project, create a teacher, and find it and assert it is not null. But it appears that setting up a test environment is not that simple.

Upvotes: -3

Views: 75

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233347

If the application mostly does CRUD operations, there's little to be gained from separating what little application code you may have from the database. In that case, I'd consider a self-hosted integration test suite the best fit.

Essentially:

  1. Set up a SQL Server Express or LocalDb database before each test
  2. Run the test
  3. Tear down the database
  4. Repeat

My article Closing database connections during test teardown contains various links to further reading and sample code bases.

Upvotes: 1

Related Questions