2016-10-10 109 views
-1

我有一個註冊頁面,這使得下面如何嘲笑我的工作對象

var registeredAccount = await _accountServiceChannel.RegisterAsync(accountContract); 

我怎麼能嘲笑返回的對象異步調用的演出,我試圖構建像我展示如下

[Test] 
public void 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)) 
     .Returns(_mockAccountContract.Object); 
    //act 
    var result = (RedirectToRouteResult)_registerController.Register(_userRegisterationViewModel).Result; 

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

但我認爲這是行不通的,因爲我想設置一個只有get屬性的對象。

回答

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")); 
} 
+0

如果起訂量,你使用甚至不會編譯 –

+0

什麼版本? – Nkosi

+0

你在哪裏得到錯誤? – Nkosi