后台-插件-广告管理-内容页广告位一(手机)

您现在的位置是:首页 > 开发类 > 问答问答

单元测试的困惑

2021-07-06 20:01:16问答人已围观

简介 <P>我对单元测试没有什么研究,只是看了很多文章说要单元测试,没什么理论。我现在遇到一个问题</P>
<DIV class=cnblogs_code><!--<br><br>Code highlighting pro

<P>我对单元测试没有什么研究,只是看了很多文章说要单元测试,没什么理论。我现在遇到一个问题</P> <DIV class=cnblogs_code><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Activity&nbsp;:&nbsp;AbstractActivity<BR><IMG id=Codehighlighter1_49_601_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_49_601_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_49_601_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_49_601_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_49_601_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_49_601_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_49_601_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_49_601_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_49_601_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_49_601_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">override</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;Execute(IObjectFactory2&nbsp;applicationContext)<BR><IMG id=Codehighlighter1_132_472_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_132_472_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_132_472_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_132_472_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_132_472_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_132_472_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_132_472_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_132_472_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_132_472_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_132_472_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;applicationContext.Resolve</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">ILog</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">().Debug(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Starting&nbsp;DownloadWebPage</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">);</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">考虑将此句放入模板方法中</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IResult&nbsp;iresult&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;applicationContext.Resolve</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">IDictionaryAdapterFactory</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">().GetAdapter</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">IResult</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.TaskId,<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iresult.WebPageSource&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;GetSource(iresult.Url,&nbsp;Encoding.GetEncoding(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">UTF-8</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">),&nbsp;</SPAN><SPAN style="COLOR: #800080">30000</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;GetSource(</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;url,&nbsp;Encoding&nbsp;encoding,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;timeout)<BR><IMG id=Codehighlighter1_558_594_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_558_594_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_558_594_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_558_594_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_558_594_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_558_594_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_558_594_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_558_594_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_558_594_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_558_594_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN></DIV> <P>该类主要有两个方法Execute,GetSource,其中Execute接收外部的对象,然后在内部调用GetSource方法进行处理。对该类进行单元测试,我的想法是,对GetSource进行单元测试,虽然其是私有方法,但是VS2008集成的单元测试工具可以对该方法进行测试,而不用改变其修饰符。这一步完成了。</P> <P>对另一个Execute方法是否要测试,我有些困惑,传入的是一个接口,我考虑使用Mock,可是mock之后,整个方法是执行了,我怎么对它进行测试呢,这个方法是void我要测试什么呢?</P>

最佳答案:你不应该测试Private的方法,即使微软为你提供了方法。因为,一个方法被定义为Private的,就表示,这个方法不应该被其它人使用。他还有一个隐含的意思:这个方法可能随时被删除和修改。这样的修改不需要通知任何人。 对于你上面的例子,如果你认为GetSource方法的已经很稳定,不会被删除或修改接口。那么,我觉的,你应该抽象一个类似GetSrouceHelper的静态类型,包装这个方法,并且Public它。对这个静态类型单独编写单元测试。 从你上面的程序看,你的Execute方法的实现已经足够的简单了,如果你对GetSource方法已经进行了测试,同时你认为将来Execute方法也不会有什么变化了,并且你对它的实现已经有信心了。就不要写单元测试了。单元测试的一个重要的原则就是:对需要测试的单元进行单元测试。 如果是在要写,可以如下: MockRepository repository = new MockRepository(); IObjectFactory2 factory = repository.CreateMock<IObjectFactory2>(); IDictionaryAdapterFactory adapter = repository.CreateMock<IDictionaryAdapterFactory>(); IResult result = repository.CreateMock<IResult>(); // 这里写期待被调用的代码 Expect.On(factory.Resolve<IDictionaryAdapterFactory>()).Return(adapter); Expect.On(adapter.GetAdapter<IResult>(123/*this.TaskId*/).Return(result); result.WebPageSource = "http://cajon.cnblogs.com/" /*你期望的URL*/; repository.ReplayAll(); // 这里写测试代码 Execute(factory); // 最后校验 repository.VerifyAll(); 看起来很无聊吧?就是因为这个方法确实已经不需要测试了。当然,如果你对IObjectFactory2不放心,那你应该去测试IObjectFactory2,而不是测试这个方法。

文章来源:https://q.cnblogs.com/q/2447/

Tags:.net技术 c 

很赞哦! ()

后台-插件-广告管理-内容页广告位二(手机)

相关文章

后台-插件-广告管理-内容页广告位三(手机)
后台-插件-广告管理-内容页广告位四(手机)

文章评论

留言与评论(共有 0 条评论)
   
验证码:

本栏推荐

站点信息

  • 文章统计90025篇文章
  • 浏览统计10083次浏览
  • 评论统计1个评论
  • 标签管理标签云
  • 统计数据:统计代码
  • 微信公众号:扫描二维码,关注我们