Tôi rất mới này (angularjs, hoa nhài, testacular) và tôi có mã này (tôi đơn giản hóa nó một chút, chỉ để lại những gì quan trọng):thử nghiệm AngularJS nhà máy chức năng với Jasmine
//my_module.js
angular.module('my_module', ['my_data'])
.config([...]);
.controller('my_controller', ['$scope', 'my_data',
function($scope, my_data) {
$scope.my_function = function() {
return my_data.my_factory.save().then(function() {
console.log('saved');
},
function() {
console.log('Error');
});
}
}
)
//my_data.js
angular.module('my_data', [])
.factory('my_factory', ['$q', '$rootScope',
function($q, $rootScope) {
var my_factory= function(my_data) {
angular.extend(this, my_data);
}
my_factory.prototype.save = function() {
var deferred = $q.defer();
setTimeout(function() {
deferred.resolve();
$rootScope.$apply();
}, 1000);
return deferred.promise;
}
return my_factory;
}
])
Vì vậy, những gì Tôi muốn làm là kiểm tra nếu my_data.my_factory.save được gọi khi my_module.my_controller.my_function được kích hoạt.
//my_test.js
describe('testing my_controller.my_function', function() {
beforeEach(module('my_module'));
var $rootScope, $controller;
beforeEach(inject(function(_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
$controller = _$controller_;
}));
scope = $rootScope.$new();
it('should call the save function', function() {
scope.my_function();
expect(my_data.save).toHaveBeenCalled();
});
}
Tôi cần một chút trợ giúp.
Xin chào @CaioCunha .. Bạn đang kiểm tra bộ điều khiển nào? Bạn chỉ định nó ở đâu? Tôi thực sự thích cách tiếp cận này, và tôi đang tái cấu trúc lại thử nghiệm của mình nhưng tôi đã nghi ngờ điều này https://github.com/Ridermansb/listfy/blob/feature/create-a-new-product-%232/tests/components /homeControllerTest.js – ridermansb