简体   繁体   English

如何在我的ASP.NET MVC项目中测试附加数据库的模型?

[英]How to test Model of attached DB in my ASP.NET MVC project?

I created a standard ASP.NET MVC project and then added a SQL Express database in the App_Data folder. 我创建了一个标准的ASP.NET MVC项目,然后在App_Data文件夹中添加了一个SQL Express数据库。 The connection string is recorded into the web.config file. 连接字符串记录在web.config文件中。 I then created a LINQ to SQL data model for the database. 然后,我为数据库创建了LINQ to SQL数据模型。 I want to test the data model from my Test project but I don't know how to go about this correctly because I am using an attached database. 我想从我的Test项目中测试数据模型,但是我不知道如何正确执行此操作,因为我正在使用附加数据库。 Since the database is attached to the MVC project and not the Test project, how would I be able to access it from the Test project? 由于数据库是附加到MVC项目而不是Test项目的,所以我如何能够从Test项目访问它?

If you're talking about Unit Tests, they ought to be able to run without the database. 如果您正在谈论单元测试,那么它们应该能够在没有数据库的情况下运行。 The idea is that your code is broken into re-usable blocks that can be individually tested by passing in mocked / stubbed data. 想法是将您的代码分成可重复使用的块,可以通过传入模拟/存根数据来对其进行单独测试。

Sohnee is correct; Sohnee是正确的; a unit test should not have dependencies on your production database. 单元测试不应依赖于您的生产数据库。 It sounds like you're looking to do some broader integration testing, perhaps? 听起来您正在寻找一些更广泛的集成测试,也许吗?

I could go into some detail on this, but I don't think it is what you're looking for. 我可以对此进行详细介绍,但我认为这不是您想要的。 Could you elaborate on what you mean or aim to accomplish by "testing the data model?" 您能否详细说明通过“测试数据模型”要实现的目标或目标?

Some schools of thought are very strict on what should go into a unit test and not. 一些思想流派对应该进行单元测试而不是进行单元测试非常严格。 Others not so much. 其他人没有那么多。

It's all about preferences, I think. 我认为,这完全取决于偏好。 Just remember that it's easy to forget to test the real thing when you mock everything out. 只要记住,当您模拟所有内容时,很容易忘记测试真实的东西。

But to answer your question, what happens if you just create the data context in your test? 但是要回答您的问题,如果仅在测试中创建数据上下文会怎样? Can you query against it, or do you get weird connection exceptions? 您可以对其进行查询,还是会得到奇怪的连接异常?

If the latter is the case, and you still want to test against your database, I guess you have to copy the database file to somewhere your test can reach it. 如果是后者,并且您仍要针对数据库进行测试,我想您必须将数据库文件复制到测试可以到达的位置。 Anyway, you can pass the connection string to the data context constructor. 无论如何,您可以将连接字符串传递给数据上下文构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编辑无法在我的 ASP.NET Core MVC 项目上正常工作 - edit not working correctly on my ASP.NET Core MVC project 如何在Asp.Net MVC视图页面中区分我的订单模型和订单sql函数 - how to differentiate between my Order Model and the order sql function in Asp.Net MVC view page 如何在 Asp.net Mvc 中显示没有 model 的数据? - How to Display Data without a model in Asp.net Mvc? 如何添加表我的连接表? ASP.NET MVC - How to add table my join tables? ASP.NET MVC 我的ASP.NET MVC项目未获取我的SQL Server Express值 - My ASP.NET MVC project is not retrieving my SQL Server Express values 为什么在我的视图中出现 null 指针错误 model ASP.NET MVC - Why am I getting a null pointer error in my view model ASP.NET MVC 如果来自两个不同数据库表的2个数据值在ASP.NET MVC 4中相等,如何有条件地向ViewData.Model添加列表? - How do i conditionally add a list to a ViewData.Model if 2 data values from two different DB tables are equal in ASP.NET MVC 4? 计算我的ASP.NET MVC项目中相同产品名称的总和 - Count total sum of the same product name in my ASP.NET MVC Project 需要帮助将SQL查询转换为LINQ语句以在我的ASP.NET MVC 3项目中使用 - Need help converting SQL query to LINQ statement for use in my ASP.NET MVC 3 project ASP.net MVC:从DB String执行Razor? - ASP.net MVC: Execute Razor from DB String?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM