关于现有实体的C#ASP.NET MVC unittesting错误。 [英] C# ASP.NET MVC unittesting error about the existing entities.

查看:61
本文介绍了关于现有实体的C#ASP.NET MVC unittesting错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用AspNet.Mvc网络应用程序,我想使用'ms unit test'测试它,这是我正在测试的控制器功能:



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);            
        }
    }
}





整数13确实在数据库中有记录,并且mvc应用程序正常运行。但是当我使用上面的测试时,它总是失败。并给我以下错误:



测试方法引发异常:

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException:存储更新,插入或删除语句影响了意外的行数(0)。实体可能已被修改或删除,因为实体已加载。



但实体存在于数据库和Web应用程序中,我可以看到它们。



我看不出问题所在。



谢谢,



我尝试了什么:



我在网上搜索它,但我看不到解决方案。



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.

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]
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[^]


首先,这不是单元测试,单元测试不需要外部资源,只测试代码的逻辑。其次,我尝试测试的所有测试都是EntityFramework,但你应该假设MS测试自己的代码,所以你不必这样做。第三,您的控制器代码不是单元可测试的。最后,您的代码在测试工具(mstest)的上下文中运行,而不是在您的站点上下文中,即当您在浏览器中查看它时,因此当Entity Framework加载它需要的任何配置时,很可能配置保存在您的网站的配置,而不是测试工具,因此它可能无法访问它作为网站运行时所做的事情。



最终你应该停止你是什么这样做,它不是单元测试,它没有增加任何价值。我应该能够在构建过程中运行单元测试来检查代码的完整性,但是如果测试失败,我怎么能这样做,因为表中没有记录13?如果您运行该单元测试,它将工作一次,再次运行它将失败,那么该测试有什么价值?当您的代码逻辑没有任何问题时,它会显示失败。如果你想实现单元测试,那么阅读一些文章并以可测试的方式编写你的代码。
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.


这篇关于关于现有实体的C#ASP.NET MVC unittesting错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆