Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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:

C#
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.
Posted
Updated 27-May-16 1:31am

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 
First of all, this isn't a unit test, a unit test requires no external resources and tests only your code's logic. Secondly all your test I trying to test is EntityFramework, but you should assume that MS test their own code so you don't have to. Thirdly your controller code is not unit testable. Lastly, your code runs under the context of the test harness (mstest) not under the context of your site, ie as you view it in your browser, so when Entity Framework loads any configuration it needs, chances are that config is held in the config of your website and not the test harness so it may not have access to things it does when run as a website.

Ultimately you should just stop what you're doing, it is not unit testing and it adds no value. I should be able to run unit tests as part of a build process to check the integrity of the code, but how can I do that if your test fails because there is no record 13 in a table? If you run that unit test it will work once, run it again and it will fail so what value is that test? It is showing a failure when there is nothing wrong with your code logic. If you want to implement unit testing then read some articles on it and write your code in a way that is testable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900