我一直在跟随John Papa和Ward Bell对PluralSight进行角度测试.
当我运行我的规格时,我目前收到以下错误.
AssertionError: expected { Object ($$state) } to have a property 'length'
at Assertion.assertLength (bower_components/chai/chai.js:1331:37)
at Assertion.assert (bower_components/chai/chai.js:4121:49)
at Context.<anonymous> (scripts/home/homeController.Specs.js:48:49)
Run Code Online (Sandbox Code Playgroud)
请注意,我只包含了我认为相关的代码,因此我没有用无关的信息重载这个问题.如果您需要查看更多代码,这不是问题.
我的代码如下:
homeController.js:
window.app.controller('homeController', ['$scope', 'sidebarService',
function ($scope, sidebarService) {
$scope.title = 'Slapdash';
$scope.sidebar = {
"items": sidebarService.getSidebarItems()
};
}])
Run Code Online (Sandbox Code Playgroud)
sidebarService.js:
(function () {
window.app
.service('sidebarService',['$http', function ($http) {
this.getSidebarItems = function () {
$http.get("http://wwww.something.com/getSidebarItems")
.then(function (response) {
return response.data;
});
};
}]);
}());
Run Code Online (Sandbox Code Playgroud)
homeController.Specs.js:
beforeEach:
beforeEach(function () {
bard.appModule('slapdash');
bard.inject(this, '$controller', '$q', '$rootScope')
var mockSidebarService …Run Code Online (Sandbox Code Playgroud) 我正在编写一个角度控制器,它对数据服务有依赖性(数据服务转到http服务器),我想模仿它的行为.
我正在使用一个名为bard js的库进行模拟,它有一个名为bard.mockService的模拟服务api .
在beforeEach语句中,我正在做:
beforeEach(function() {
bard.appModule('app.process.creation');
bard.inject('$controller', '$rootScope', '$q', 'processCreationDataservice');
bard.mockService(processCreationDataservice, {
createProcess: $q.when({}),
_default: $q.when([])
});
controller = $controller('ProcessCreationController');
$rootScope.$apply();
});
Run Code Online (Sandbox Code Playgroud)
然后我的测试:
it('should call create process data service to create process', function() {
controller.create();
expect(processCreationDataservice.createProcess).to.have.been.calledOnce;
});
Run Code Online (Sandbox Code Playgroud)
正如您在测试中看到的那样,我想声明dataservice.createProcess被调用一次.
控制器没有调用方法processCreationDataservice.createProcess,仍然测试正在通过.
(function() {
angular
.module('app.process.creation')
.controller('ProcessCreationController', ProcessCreationController);
ProcessCreationController.$inject = ['processCreationDataservice'];
function ProcessCreationController(processCreationDataservice) {
var vm = this;
vm.process = {
name: '',
bankId: ''
};
vm.create = function() {
};
}})();
Run Code Online (Sandbox Code Playgroud)
我想知道为什么这个测试正在通过,我该怎么做才能断言该方法被调用一次.
我按照这些说明操作:https: //github.com/wardbell/bardjs#mockService