如何测试名为“示例”的 Angular 服务.Svc' 在我的模块中使用 mocha+chai

how to test a angular service which named as 'sample.Svc' in my modules using mocha+chai

本文关键字:Svc 我的 模块 mocha+chai 服务 Angular 测试 何测试 示例      更新时间:2023-09-26

我的服务文件服务.js

angular.module('services', [])
  .service('sample.Svc', ['$window', 'modalSvc', function($window, modalSvc){
    this.showDialog = function(message, title){
      console.log(" in here");
      if(title){
        modalSvc.showModalDialog({
          title: title,
          message: message
        });
      } else {
        $window.alert(message);
      }
    };
  }]);

我的服务规格.js

describe('service tests', function(){
  var mockWindow, mockModalSvc, sampleSvcObj;
  beforeEach(function(){
    module(function($provide){
      $provide.service('$window', function(){
        this.alert = sinon.spy();
      });
      $provide.service('modalSvc', function(){
        this.showModalDialog = sinon.spy();
      });
    });
    module('services');
  });
  beforeEach(inject(function($window, modalSvc, sample.Svc){
    mockWindow=$window;
    mockModalSvc=modalSvc;
    sampleSvcObj=sampleSvc;
  }));
 it('Need to pass', function() {
        expect(false).to.be.false;
    });
});

运行我的serviceSpec.js时(使用 Karma(
我收到类似这样的错误

"语法错误:解析错误"。

我知道由于参数名称(示例(引起的此错误。Svc( 与 ' . '。但是我不知道在我的测试用例中使用我的服务"sample.Svc"的其他方法。有些人帮助我以任何其他方式注入我的服务。

服务是由其他人使用点命名的,我有权更改它们。所以我需要一些具有相同命名结构的解决方案。
提前谢谢。

尝试使用命名约定的最佳做法。您将节省大量时间和头痛

https://github.com/mgechev/angularjs-style-guide#services

编辑:详细说明,称您的服务为"示例"或类似的东西。