使用Typemock验证带有精确参数的方法调用

时间:2016-03-09 08:54:55

标签: c# typemock

我有一个方法:

[TestMethod, Isolated]
public void TestArgs()
{
    var bar = new Bar();

    Isolate.WhenCalled(() => bar.Foo()).CallOriginal();

    string arg1 = "Apple";
    string arg2 = "Pear";
    string arg3 = "Potato";
    bar.Foo(arg1, arg2, arg3);

    Isolate.Verify.WasCalledWithArguments(() => bar.Foo(null, null, null)).Matching(a =>
        (a[0] as string).Equals(arg1) &&
        (a[1] as string).Equals(arg2) &&
        (a[2] as string).Equals(arg3)
     );
}

我想验证它是用正确的参数调用的。我已完成所有操作,如docs

<?php
$sql= "SELECT * FROM form WHERE id='$formID'";
$result = mysql_query($sql) or die('Cannot get ID. ' . mysql_error()); 
$row=mysql_fetch_array($result);

$staffID=$row['staff_id'];
$sql2= "SELECT * FROM users WHERE staff_id='$staffID'";
$result2 = mysql_query($sql2); 
$row2=mysql_fetch_array($result2);
?>

<form action="viewProcess.php?action=modifyView&id=<?php echo $row['id']?>" method="post" enctype="multipart/form-data" >
<h3 class="underlineLongest">USER APPLICATION FORM </h3>
<p align="right">Reference Number : <b> <?php echo $row['ref_no']; ?> </b> </p>
<table width='100%'>
    <tr> 
        <td  colspan='2' bgcolor="#C7C7C7"> Applicant Details</td>
    </tr>
    <tr> 
        <td width='30%'>Date of Application </td>
        <td width='70%'> <textColor> <?php echo $row['app_date']; ?> </textColor> </td>
    </tr>
    <tr> 
        <td width='30%'>User Full Name </td>
        <td width='70%'> <textColor> <?php echo $row2['name']; ?> </textColor> </td>
    </tr>
    <tr> 
        <td width='30%'>Designation/Staff ID </td>
        <td width='70%'> <textColor> <?php echo $staffID; ?> </textColor> </td>
    </tr>
    <tr> 
        <td width='30%'>Department/Division </td>
        <td width='70%'> <textColor> <?php echo $row2['department'].'/'.$row2['division']; ?> </textColor> </td>
    </tr>
    <tr> 
        <td width='30%'>Telephone Ext. No </td>
        <td width='70%'> <textColor> <?php echo $row2['ext']; ?> </textColor> </td>
    </tr>
<tr> 
    <td  colspan='2' bgcolor="#C7C7C7" > Application Service Required </td>
</tr>
    <tr> 
    <td width='30%'>Type of Application </td>
    <?php
    $type= $row['type'];
    $result4 = mysql_query(" SELECT * FROM type WHERE type_code='$type'"); 
    $row4=mysql_fetch_array($result4); ?>
    <td width='70%'> <textColor> <?php echo $type.' - '.$row4['description']; ?> </textColor> </td>
</tr>
<tr> 
    <td width='30%'> Type of Facility </td>
    <td width='70%'>
        <textColor> <?php
         $facility=explode(';',$row['facility']);

              foreach($facility as $i =>$key) 
              {
                echo $key .' - ';
                    $result5 = mysql_query(" SELECT * FROM facility WHERE fac_code='$key'"); 
                    while($row5=mysql_fetch_array($result5))   
                    {
                        echo $row5['description'].' <br> ';
                    }
              } ?>
        </textColor> </td>
</tr>

<tr> 
    <td  colspan='2' bgcolor="#C7C7C7" > Endorsed Status (Head of Department) </td>
</tr>
<tr> 
    <td width='30%'>Endorsed By </td>
    <td width='70%'> <textColor> <?php echo $row['endorsed_by']; ?> </textColor> </td>
</tr>
<tr> 
    <td width='30%'>Status </td>
    <td class="alt3">
        <input name="radiobutton1" type="radio" value="APPROVED" checked >Approve
        <input name="radiobutton2" type="radio" value="REJECTED" >Reject
    </td>
</tr>
</tr>
<tr> 
    <td width='30%'>Date & Time </td>
    <td width='70%'> <textColor> <?php echo $row['endorsed_time']; ?> </textColor> </td>
</tr>
<tr> 
    <td width='30%'>Remarks </td>
    <!--<td width='70%'><textarea name="endorsed_remark" id = "endorsed_remark"></textarea> </td> -->
    <td><input type="text" name="endorsed_remark" id="endorsed_remark" /></td>      
</tr>

</table>

<br><br>
<center><p><input class="btnSuccess" type ="submit" name="submit" value="Submit" onclick="modifyView();" >  
   &nbsp;&nbsp;<input class="btnEdit" type="button" name="btnCancel"  value="Cancel" onclick="goBack()" > </p>

但我得到例外:

  

System.NullReferenceException:对象引用未设置为   对象的实例。

有人可以告诉我为什么我会得到它吗?

2 个答案:

答案 0 :(得分:2)

免责声明,我在Typemock工作。

关键是你正在使用'params'关键字。它将所有参数包装在一个对象中 - 参数数组。因此,为了正确验证'params',请使用以下语句:

 Isolate.Verify.WasCalledWithArguments(() => bar.Foo(null)).Matching(a =>
     (a[0] as string[])[0].Equals(arg1) &&
     (a[0] as string[])[1].Equals(arg2) &&
     (a[0] as string[])[2].Equals(arg3)
 );
祝你好运!

答案 1 :(得分:1)

你试过调试吗?

尽管如此,如果您将最后一个语句更改为

Isolate.Verify.WasCalledWithExactArguments(() => bar.Foo(arg1, arg2, arg3));

测试将通过。