jeudi 24 septembre 2015

Invalid object name even though the object is valid

I have a unit test for a controllers actionresult. When I run the unit test it breaks at db.SaveChanges(); part of the action result. When I test the actual method (via web brower and data in database) it is fine however when I run my unit test it fails. The error I am receiving is: DbUpdateException was unhandled by user code. See inner exception, Inner Exception: {"Invalid object name 'dbo.Projects'."} I am using entityframeworks codefirst structure and here is my code:

[ValidateAntiForgeryToken]
        [Authorize(Roles = "A")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddProject(Projects project)
        {
            if (ModelState.IsValid)
            {
                using (var db = new MyDbContext())
                {
                    db.ProjectModels.Add(project);
                    db.SaveChanges();
                }
                return RedirectToAction("ListOfProjects", "Project");
            }
            return View(project);
        }


[TestMethod()]
public void AddProjectTestWithModl()
{
    var controller = new AdminController();
    var model = new Projects() { Name = "Name", Description = "Desc", Duration = "1 month" };
    var result = controller.AddProject(model) as ViewResult;
    Assert.AreEqual(model, result.ViewData.Model);
}

Aucun commentaire:

Enregistrer un commentaire