我试图断言异步方法中发生的异常 不幸的是,断言不起作用。
我确保通过调试确保在throw new Exception(...)
方法中调用SaveCategoriesAsync(...)
。
使用Action
的方法通常适用于单元测试...但似乎它不能用于异步。
我使用 FakeItEasy 作为模拟框架,使用 FluentAssertions 作为断言框架。
[TestMethod]
public void SaveCategoriesAsync_When_Then()
{
A.CallTo(() => this.articleRepository.GetArticles(A<IList<long>>._)).Returns(new List<ArticleModel>());
Action action = async () => await this.testee.SaveCategoriesAsync(new List<int>());
action.ShouldThrow<Exception>();
}
我必须更改什么才能断言异常?
提前致谢
答案 0 :(得分:1)
答案实际上与我的尝试非常相似。但是使用异步,您必须使用<head>
<!--ajax library-->
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script>
//angular module code
angular
.module('myApp', [])
.run(function($rootScope) {
$rootScope.title = 'myTest Page';
})
.controller('testController', ['$scope',
function($scope) {
//dummy array data
$scope.mydata = [
{ "cID": 1014,"country": "USA","state" :"California"}
];
//country json to get selected state and city based on country
$scope.countries = {
'USA': {
'Alabama': ['Montgomery', 'Birmingham'],
'California': ['Sacramento', 'Fremont'],
'Illinois': ['Springfield', 'Chicago']
},
'Australia': {
'New South Wales': ['Sydney'],
'Victoria': ['Melbourne']
}
};
$scope.GetSelectedCountry = function() {
$scope.strCountry = $scope.country;
};
$scope.GetSelectedState = function() {
$scope.strState = $scope.state;
};
}
])</script></head>
<!--HTML Code-->
<body ng-app="myApp">
<div ng-controller="testController"> <label for="country">Country
*</label> <select id="country" name="country" ng-model="mydata .country" ng-options="country for (country, states) in countries"
ng-change="GetSelectedCountry()">
<option value=''>Select</option></select> <label for="state">State *</label> <select id="state"
ng-disabled="!country" name="state" ng-model="mydata.state"
ng-options="state for (state,city) in country"
ng-change="GetSelectedState()">
<option value=''>Select</option></select> <label for="city">City *</label> <select id="city" ng-disabled="!country
|| !state" ng-model="city" ng-options="city for city in state">
<option value=''>Select</option>
</select>
</div>
</body>
而不是操作:
Func<Task>
答案 1 :(得分:-1)
如果很容易更改测试运行器,请考虑NUnit。然后你的代码看起来:
...
Assert.ThrowsAsync<Exception>(async () => await this.testee.SaveCategoriesAsync(new List<int>());
...