Hi,
I'm using AspNet.Mvc web application which I'd like to test it using 'ms unit test' here is the controller function which i'm testing:
public ActionResult Delete(int id)
Student studentToDelete = new Student() { ID = id };
db.Entry(studentToDelete).State = EntityState.Deleted;
db.SaveChanges();
return RedirectToAction("Index");
}
Here is my test class:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ContosoUniversity;
using System.Web.Mvc;
using ContosoUniversity.Controllers;
namespace UnitTestProject
{
[TestClass]
public class StudentControllerTest
{
[TestMethod]
public void Delete()
{
ContosoUniversity.Controllers.StudentController
st = new ContosoUniversity.Controllers.StudentController();
st.Delete(13);
}
}
}
the integer 13 does have a record in the database, and the mvc application is working properly. but when i use the test above it always fails. and gives me the following error:
Test method threw exception:
System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: Store update,insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.
But the entities exist in the DB and in the web application I can see them.
I cannot see where the problem is.
Thanks,
What I have tried:
I'm searching the net for it, but yet i see no solutions.