Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJSUnitTesting:预期已调用spy函数()_Angularjs_Unit Testing_Spy - Fatal编程技术网

AngularJSUnitTesting:预期已调用spy函数()

AngularJSUnitTesting:预期已调用spy函数(),angularjs,unit-testing,spy,Angularjs,Unit Testing,Spy,下面是我的代码,我不想调用服务函数,所以我使用的是spy,但它给出了错误。我无法找出它 'use strict'; describe('Testing DetailCtrl\n\n\n', function() { beforeEach(module("safe-repository")); var $controller, $scope, controller; var services = { documentService:null }; // Initi

下面是我的代码,我不想调用服务函数,所以我使用的是spy,但它给出了错误。我无法找出它

'use strict';

describe('Testing DetailCtrl\n\n\n', function() {

  beforeEach(module("safe-repository"));

  var $controller, $scope, controller;
  var services = {
   documentService:null
  };

  // Initialization before tests
  beforeEach(inject(function(_$controller_, _documentService_){
    $controller = _$controller_;
    $scope = {};
    controller = $controller('DetailCtrl', { $scope: $scope });
    services.documentService=_documentService_;

    spyOn(services.documentService, 'deleteDocument').and.callFake(function(){
           console.log("inside delete function");
    });

  }));


  describe('Testing self.deleteFile() function for different test cases\n\n', function() {


    it(' When user has access permission to delete file/doc', function(done) {
        expect(services.documentService.deleteDocument).toHaveBeenCalled();
        // Inform jasmine that the test finish here
        done();
    });

  });


});
感谢您的帮助

你不需要

var services = {
   documentService:null
};
那个代码只是把事情弄糊涂了

你应该把它简化为

// services.documentService=_documentService_; // WHY DO THIS??
documentService=_documentService_;
然后

spyOn(documentService, 'deleteDocument').and.callFake ... etc
expect(documentService.deleteDocument).toHaveBeenCalled();
然后

spyOn(documentService, 'deleteDocument').and.callFake ... etc
expect(documentService.deleteDocument).toHaveBeenCalled();
你可能也想试试

spyOn(loginService, 'isSuperAdmin').and.returnValue("something");
而不是callFake(您的expect语句将保持不变)

还有

我假设您的控制器在构造期间正在对该方法进行预期调用?e、 g以下线路在施工期间发出预期呼叫

 controller = $controller('DetailCtrl', { $scope: $scope });
换句话说,你的控制器应该看起来像

app.controller("DetailCtrl", function($scope, documentService) {
    // some other code
    documentService.deleteDocument(); // MAKE SURE THIS CODE IS ACTUALLY BEING HIT IF ITS WRAPPED IN A CONDITIONAL STATEMENT
    // some other code
});

试试这个,它可能会帮助您:)


您是否真的在任何地方调用了
services.documentService.deleteDocument()
?我不确定,但是的,我想调用services.documentService.deleteDocument(),并将其模拟为testYeah刚才我观察到,我认为调用没有发生在服务内部,我在注入或分配过程中犯了什么错误