NUnit用于数据库检索

编程入门 行业动态 更新时间:2024-10-21 03:48:28
本文介绍了NUnit用于数据库检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个返回List< user>的数据层其中用户"是一个类对象.为此编写Nunit测试方法的正确方法是什么.我是Nunit的新手,我尝试使用Assert调用进行编写.这看起来不够. 我不确定在这里我该如何处理Nunit.任何帮助表示赞赏. 非常感谢!

Hi, I have a data layer which returns List<user> where "User" is a class object. What is the correct way to write Nunit test method for this. I am new to Nunit, I attempted to write using Assert calls. This looks insufficient. Iam unsure how I have to handle Nunit here. Any help appreciated. Many thanks!

private string s_connection = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + FormHelperClass.DataBaseName + ";Jet OLEDB:System Database = " + FormHelperClass.SystemDataBasePath + ";User Id=userid;Password=password"; public List<User> GetAllUsers() { List<User> users = new List<User>(); try { //query the database string query = "SELECT UserID, UserName FROM User order by UserId;"; // setup the connection OleDbConnection connection = new OleDbConnection(s_connection); connection.Open(); OleDbCommand command = new OleDbCommand(query, connection); OleDbDataReader reader = command.ExecuteReader(); // loop through reader & read configs while (reader.Read()) { User user = new User(); user.UserId = Convert.ToInt16(reader["UserID"]); user.UserName = (reader["UserName"]).ToString(); users.Add(user); } reader.Close(); connection.Close(); } catch (Exception ex) { log.Debug("Error Message " + ex.Message); } return users; } using NUnit.Framework; [TestFixture] public class ServiceProvidersTest { [Test] public void TestGetUsers() { FormHelperClass.DataBaseName = "C:\\Users\\Security.mdb"; UserManager u_manager = new UserManager(); List<User> list_manager = u_manager.GetAllUsers(); Assert.IsNotNull(list_manager); Assert.Greater(list_manager.Count, 0); foreach(User user in list_manager) { Assert.IsNull(user); } } }

推荐答案

可以使用多种方法对DAL进行单元测试. 选项1:内存中数据库 使用内存数据库进行测试,然后再次运行所有测试.最好选择一个内存数据库,该数据库提供的语法几乎与您所使用的实际数据库相似. 选项2:模拟ADO.Net OleDbConnection具有基础接口IDbConnection OleDbCommand具有IDbCommand等. 您可以使用DI,可以自动使用容器,也可以使用手动,也可以使用工厂实现,以便在运行时注入正确的依赖项. 选项2可能是一个昂贵的选择,我建议仅在您的DAL必须解决复杂的层次结构/依赖项时才采用. 选项3: 跟随链接 对数据访问层进行单元测试 [ ^ ] 对数据访问层2进行单元测试 [ ^ ]看看这是否满足您的需求. There are different ways you can perform unit testing of DAL. Option 1: In-memory database Using an in-memory database and for testing run all the tests agains it. It is best if you can choose an in-memory database with which offers almost similar syntax as the actual database you are using. Option 2: Mocking the ADO.Net OleDbConnection has an underlying interface IDbConnection OleDbCommand has IDbCommand and so on. You can use DI, automatic using containers or manual or a factory implementation would also do, to inject the right dependency depending at runtime. Option 2 could be an expensive choice, I suggest take it only if your DAL has to resolve complex hierarch / dependencies. Option 3: Follow the link Unit Testing the Data Access Layer[^] Unit Testing the Data Access Layer 2[^] see if this suites your need.

没有正确的方法编写测试和大多数时间效率无关紧要,因为该工具可以完成所有工作. 您必须注意,编写出色的测试不是目标,但是目标是编写出色的软件,因此测试仅用于支持主要代码. 校长应遵循笔试: 1)它们应该是可重复的,因此您必须在安装之前和之后都具有设置和清除代码. 2)尽可能细粒度,测试特定功能比一次测试所有功能要好. There is no correct way of writing test and most of the time efficiency is of no concern as the tool is doing all the work. You must note that writing great tests is not the goal, but the goal is writing great software so the test are only there to support the main code. Principals to follow in writing test: 1) They should be repeatable, so you must have setup and cleanup code before and after. 2) Be as granular as you need to be, its better to test a specific feature than to test everything at once.

更多推荐

NUnit用于数据库检索

本文发布于:2023-07-08 20:13:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080257.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   NUnit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!