如何模拟我的Task对象

时间:2016-10-10 18:03:55

标签: c# mocking task-parallel-library task moq

我有一个注册页面,可以进行异步调用,如下所示

<body bgcolor="#E6E6FA">
  <form id=orders name="orders" action="orders_action.php" method="post" enctype="multipart/form-data" onsubmit="return Validate(this);">
    <table align="">
      <tr>
        <td height="40">
          <br>
          <span class="headingbox" id="hBoxNIC"> National ID </span>
          <span style="width:100%;text-align:center;">
            <input type="text" placeholder="920290505v" maxlength="13" name=NIC required autofocus />
          </span>
        </td>
      </tr>
      <tr>
        <td height=50>
          <span class="headingboxs">Pick up</span>
          <input type=radio name=DP required value="Pickup">
          <span style="  float: right;">
            <span class="headingboxs">
              Delivery</span>
          <input class="textfill" type=radio name=DP required value="Delivery" onmouseup="textbox(this)" />
          </span>
          <br>
          <br>
        </td>
      </tr>
      <tr>
        <td height="50">
          <span class="headingbox">Expected Time </span>
          <span style="width:100%;text-align:center;">
            <input type="time" id=time autofocus name=DPTime onfocusout="hid('timeerror2');" onfocus="show('timeerror2');" min="09:00:00" max="22:00:00" />
            <br>
          </span>
          <span class="poperror" id="timeerror2"> Pharmacy is opened from 9AM to 10PM </span>
        </td>
      </tr>
      <tr>
        <td height="50">
          <span class="headingbox" id="hBoxPN"> Phone Number </span>
          <span style="width:100%;text-align:center;">
            <input type="text" maxlength=10; autofocus name=Tele />
            <br>
          </span>
          <span class="error" id="phoneerror"> error occured </span>
          <br>
        </td>
      </tr>
      <tr>
        <td height="50">
          <span class="headingbox" id="hBoxEM"> E-mail </span>
          <span style="width:100%;text-align:center;">
            <input type="text" autofocus name=Email placeholder="xxx@gmail.com" />
            <br>
          </span>
          <span class="error" id="emailerror"> error occured </span>
          <br>
        </td>
      </tr>
      <tr>
        <td height="50" width=330>
          <br>
          <span class="headingbox"> Prescription Copy-1</span>
          <span id="kids">
            <input id="uploadFile" class="disableInputField" placeholder="Choose File" disabled="disabled" />
            <label class="fileUpload">
              <input id="uploadBtn" type="file" class="upload" name=Image1 />
              <span class="uploadBtn">Upload</span>
          </label>
          <input type="button" id="add" onclick="addkid()" value="+" />
          </span>
        </td>
      </tr>
      <tr>
        <td colspan=5 align=center>
          <input class="button" type=submit name=submit value=Place>
          <input class="button" type=reset name=reset value=Cancel>
        </td>
      </tr>
    </table>
  </form>
</body>

我如何模拟返回的对象,我试图构建如下所示的

boolean valid = false;
for (int i = 999*999; i > 100*100; i--) {
  if (i / 100000 == i % 10 &&
      i / 10000 % 10 == i / 10 % 10 &&
      i / 1000 % 10 == i / 100 % 10) {
        int buffer = i;
        int total = 1;
        for (int k = 2; k < buffer; k++) {
          if (buffer % k == 0) {
            buffer /= k;
            total *=k;
          }
        }
        if (buffer >= 100 && buffer < 1000 && total >=100 && total < 1000 ) {
          System.out.println(i);
          System.out.println(total);
          System.out.println(buffer);
          break;
        }
     }
    }
  }
}

但是我觉得它没有用,因为我试图设置一个只有get属性的对象。

1 个答案:

答案 0 :(得分:2)

使用ReturnsAsync

_mockAccountServiceChannel
        .Setup(svc => svc.RegisterAsync(accountContract))
        .ReturnsAsync(_mockAccountContract.Object);

你也应该让测试同步

[Test]
public async Task Display_Validate_Account_Page_On_Successful_Registration()
{
    //arrange
    var businessContract = Mapper.Map<BusinessViewModel, BusinessContract>(_userRegisterationViewModel.Business);
    var userContract = Mapper.Map<UserViewModel, UserContract>(_userRegisterationViewModel.User);

    var accountContract = new AccountContract()
    {
        Business = businessContract,
        User = userContract
    };


    _mockAccountContract
        .Setup(contract => contract.Result.Value.Business)
        .Returns(accountContract.Business);

    _mockAccountContract
        .Setup(contract => contract.Result.Value.User)
        .Returns(accountContract.User);

    _mockAccountServiceChannel
        .Setup(svc => svc.RegisterAsync(accountContract))
        .ReturnsAsync(_mockAccountContract.Object);

    //act
    var result = (RedirectToRouteResult) await _registerController.Register(_userRegisterationViewModel);

    //assert
    Assert.That(result.RouteValues["action"], Is.EqualTo("ValidateAccount"));
}