单元测试方法无效? [英] Unit testing void methods?

查看:147
本文介绍了单元测试方法无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是单元的最佳方式测试不返回任何东西的方法?特别是在C#。

What is the best way to unit test a method that doesn't return anything? Specifically in c#.

什么我真的想测试是需要一个日志文件,并解析其特定字符串的方法。然后将字符串插入数据库。尚未出现,但作为非常新做什么,以TDD我想知道是否有可能测试这还是东西并没有真正得到测试。

What I am really trying to test is a method that takes a log file and parses it for specific strings. The strings are then inserted into a database. Nothing that hasn't been done before but being VERY new to TDD I am wondering if it is possible to test this or is it something that doesn't really get tested.

推荐答案

如果方法不返回任何东西,它是以下两种之一

If a method doesn't return anything, it's either one of the following


  • 必要 - 你要么要求对象做一些事情本身..例如改变状态(不期望任何确认..其假定是,它会做)

  • 信息 - 只是通知有人说,事情发生(不期待的动作或响应)分别。

  • imperative - You're either asking the object to do something to itself.. e.g change state (without expecting any confirmation.. its assumed that it will be done)
  • informational - just notifying someone that something happened (without expecting action or response) respectively.

势在必行方法 - 你可以验证是否确实执行了任务。验证是否状态改变确实发生了。例如。

Imperative methods - you can verify if the task was actually performed. Verify if state change actually took place. e.g.

void DeductFromBalance( dAmount )

可以通过验证测试,如果平衡后此消息确实比dAmount

can be tested by verifying if the balance post this message is indeed less than the initial value by dAmount

信息化的方法 - 是罕见的为对象的公共接口的成员......因此无法正常单元测试。但是,如果你一定要,你可以验证是否在通知中做处理发生。例如。

Informational methods - are rare as a member of the public interface of the object... hence not normally unit-tested. However if you must, You can verify if the handling to be done on a notification takes place. e.g.

void OnAccountDebit( dAmount )  // emails account holder with info

可以通过,如果正在发送的电子邮件验证测试

can be tested by verifying if the email is being sent

发表更多的细节您的实际方法,人们将能更好地回答。

更新:你的方法是做两件事情。我其实它拆分成,现在可以独立测试两种方法。

Post more details about your actual method and people will be able to answer better.
Update: Your method is doing 2 things. I'd actually split it into two methods that can now be independently tested.

string[] ExamineLogFileForX( string sFileName );
void InsertStringsIntoDatabase( string[] );

的字符串[]可容易地通过提供第一种方法与虚拟文件和预期弦证实。第二个是稍微棘手的..你可以使用一个模拟(Google或嘲讽的框架搜索计算器),以模仿DB或打实际DB和验证,如果字符串是在正确的位置插入。检查此线程一些好的书......如果你'我会电子书籍务实单元测试重新在紧缩。

在code,将用于像

String[] can be easily verified by providing the first method with a dummy file and expected strings. The second one is slightly tricky.. you can either use a Mock (google or search stackoverflow on mocking frameworks) to mimic the DB or hit the actual DB and verify if the strings were inserted in the right location. Check this thread for some good books... I'd recomment Pragmatic Unit Testing if you're in a crunch.
In the code it would be used like

InsertStringsIntoDatabase( ExamineLogFileForX( "c:\OMG.log" ) );

这篇关于单元测试方法无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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