我是AngularJs的新手并且正在进行单元测试。我在编写的测试中遇到了这个问题并且它给了我这个错误,我意识到括号中可能存在一些错误,但我只是给出了代码示例以了解我需要如何测试它。
Expected spy $broadcast to have been called with [ 'dataSamplesReceived', Object({ newDataSamples: Object({ }) }) ]
控制器
app.controller('fileUploadCtrl',
function($scope, $rootScope, $log, fileUpload) {
$scope.uploadFile = function () {
var file = $scope.sampleFile;
$rootScope.$broadcast("sampleFilesSelected", {
newSampleFiles: file
});
.success(function (data) {
$rootScope.$broadcast("dataSamplesReceived", {
newDataSamples: data
});
})
.error(function (data) {
alert("Upload failed");
});
} // uploadFile
spec.js
describe('test broadcast for new file opload ', function () {
var $controller, $rootScope;
beforeEach(function () {
module('app');
inject(function (_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
spyOn($rootScope, '$broadcast').and.callThrough();
$controller = _$controller_;
});
});
it("should select/upload new sample files", function () {
$controller('fileUploadCtrl', {
$scope: $rootScope.$new(),
file: {}
})
expect($rootScope.$broadcast).toHaveBeenCalledWith('sampleFilesSelected', {newSampleFiles: {}})
});
it("should recieve data samples", function () {
$controller('fileUploadCtrl', {
$scope: $rootScope.$new(),
data: {}
})
expect($rootScope.$broadcast).toHaveBeenCalledWith('dataSamplesReceived', {newDataSamples: {}})
});
})
答案 0 :(得分:0)
这对我有用......
spyOn(state, 'go').and.callThrough();
// blah blah
expect(state.go).toHaveBeenCalledWith('home');
试试......
spyOn($rootScope, '$broadcast').and.callThrough();
expect($rootScope.$broadcast).toHaveBeenCalledWith('dataSamplesReceived', {newDataSamples: {}})