悬赏园豆:20
[已解决问题]
解决于 2008-08-21 19:13
<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"> </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> Activity : 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> </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> </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">override</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> Execute(IObjectFactory2 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> </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> applicationContext.Resolve</SPAN><SPAN style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">ILog</SPAN><SPAN style="COLOR: #000000">></SPAN><SPAN style="COLOR: #000000">().Debug(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Starting 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"> IResult iresult </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> applicationContext.Resolve</SPAN><SPAN style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">IDictionaryAdapterFactory</SPAN><SPAN style="COLOR: #000000">></SPAN><SPAN style="COLOR: #000000">().GetAdapter</SPAN><SPAN style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">IResult</SPAN><SPAN style="COLOR: #000000">></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> iresult.WebPageSource </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> GetSource(iresult.Url, Encoding.GetEncoding(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">UTF-8</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">), </SPAN><SPAN style="COLOR: #800080">30000</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000"> GetSource(</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000"> url, Encoding encoding, </SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> 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> </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> </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top> }</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> }</SPAN></SPAN></DIV>
<P>该类主要有两个方法Execute,GetSource,其中Execute接收外部的对象,然后在内部调用GetSource方法进行处理。对该类进行单元测试,我的想法是,对GetSource进行单元测试,虽然其是私有方法,但是VS2008集成的单元测试工具可以对该方法进行测试,而不用改变其修饰符。这一步完成了。</P>
<P>对另一个Execute方法是否要测试,我有些困惑,传入的是一个接口,我考虑使用Mock,可是mock之后,整个方法是执行了,我怎么对它进行测试呢,这个方法是void我要测试什么呢?</P>
问题补充:
可能我对问题描述的不太清楚,GetSource方法,可能测试一下各种边界条件,其输出是否是期望的就可以了(是这样吗?)
那么Execute方法(由于其传入参数是一个Interface,因此我需要隔离它,所以要mock(是这样的逻辑吗?),可是在mock了之后,我要验证什么呢,它没有返回值(这个方法我的理解是肯定要测试,可是我不清楚,单元测试的“度”是什么,要测试什么)