我如何在C#中进行单元测试? [英] how do I unit testing in C# ?

查看:173
本文介绍了我如何在C#中进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我如何进行单元测试,如果我有这个:



我写的关于此的代码:有客户,需要查找具有相同名称,地址或电话的客户(可以是2个或更多),然后需要从中选择2个客户,并将它们合并(第二个客户将从db,但是他们的属性将附加到客户,已经合并。)



现在我要检查,如果其他客户从数据库中删除,他们的属性(帐户,姓氏)是否附加到合并的客户或没有?



  public   int  MergeCustomer(客户客户, int  deletedCustomerID){
}

_db.SaveChanges();
scope.Complete();







感谢您的帮助。

解决方案

plaese见这里。我认为它可以帮助你

http://msdn.microsoft.com/en-us/library/hh270865.aspx [ ^ ]


NUnit相当不错。测试转轮易于使用。请参阅此文章,

使用NUnit进行单元测试 [< a href =http://www.codeproject.com/Articles/178635/Unit-Testing-Using-NUnittarget =_ blanktitle =New Window> ^ ]


你选择了一个困难的部分,想要测试的数据层。

一般来说,你必须设计可测试性,特别是有一个datamanager类来执行逻辑并且有一些仅用于存储和读取的受保护方法,例如那么你可以派出那个类并为你的单元测试目的实现一个ram实例。



这描述了游戏中的原理以及你需要做什么才能进行自己的练习您可以开始模拟数据层组件。

http:// msdn。 microsoft.com/en-us/magazine/dd263069.aspx [ ^ ]



另一个方面是模拟某些电话的能力(或者)这里有很多提供商的方式可用但请参阅 < a href =http://www.codeproject.com/Articles/686935/Unit-testing-with-Mock-objects-Rhino-Mocks> http://www.codeproject.com/Articles/686935/Unit-testing -with-模拟对象-犀牛-嘲笑

Hello,

How do i unit testing, if i have this:

I wrote code about this: Have "customer", need find customers, that have same name, address, or phone(they can be 2 and more) , then need choose from them 2 customers, and merge them both(second customers will be deleted from db, but them property will attach to customer, that already was merged.)

Now i want check , if others customers deleted from db, them property(account,surname, and etc.) was attach to merged customer or no?

public int MergeCustomer(Customer customer, int deletedCustomerID){
}

_db.SaveChanges();
 scope.Complete();




Thank you for help.

解决方案

plaese see here. I think it may helps You
http://msdn.microsoft.com/en-us/library/hh270865.aspx[^]


NUnit is quite good. The test runner is easy to use. See this article,
Unit Testing Using NUnit[^]


You've chosen a difficult section, the data layer to want to test.
Generally you will have to design for testability, specifically having a datamanager class which does the logics and have some protected method for just storing and readling e.g. then you can derive that class and implement a ram instance for your unit testing purposes.

This describes the principles in play and it what you need to make your own practice before you can begin to mock out data layer components.
http://msdn.microsoft.com/en-us/magazine/dd263069.aspx[^]

another aspect is the ability to 'mock out' certain calls (alternatively) here a lot of provider ways are available but see a good article here http://www.codeproject.com/Articles/686935/Unit-testing-with-Mock-objects-Rhino-Mocks


这篇关于我如何在C#中进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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