我想测试以下控制器:
angular.module('myApp').controller('OrderController', ['$scope', 'retryRequest', 'ORDER_STATUS_URL', 'MAX_RETRY',
function ($scope, retryRequest, ORDER_STATUS_URL, MAX_RETRY) {
retryRequest(ORDER_STATUS_URL, 'GET', MAX_RETRY).then(function(data) {
$scope.order = data;
}).catch(function(data) {
$scope.error = data.ErrorMessage;
});
}]);
试验:
describe('Order Controller', function () {
beforeEach(module('myApp'));
var $controller, $q;
beforeEach(inject(function (_$controller_, _$q_) {
$controller = _$controller_;
$q = _$q_;
}));
describe('When I access order data', function() {
it('I expect Id to be set', function() {
var mockRetryRequest = function(){
var deferred = $q.defer();
deferred.resolve({ 'OrderId': 'foo' });
return deferred.promise;
};
var $scope = {};
var controller = $controller('OrderController', { $scope: $scope, retryRequest: mockRetryRequest });
assert.equal($scope.order.OrderId, 'foo');
});
});
});
导致$ scope.order未定义。注入模拟,但承诺不会调用回调(然后)。
可能的候选人是摩卡设置。
bower.json
"name": "client",
"private": true,
"dependencies": {
"chai": "~1.8.0",
"mocha": "~1.14.0",
"angular": "1.3.11",
"angular-mocks": "1.3.11",
"sinon": "~1.12.2"
答案 0 :(得分:1)
摘要循环不会运行。致电$rootScope.$digest();