【问题标题】:how to test a angular service which named as 'sample.Svc' in my modules using mocha+chai如何使用 mocha+chai 在我的模块中测试名为“sample.Svc”的角度服务
【发布时间】:2016-02-15 09:51:30
【问题描述】:

我的服务文件 service.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);
      }
    };
  }]);

我的 serviceSpec.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)
我收到类似的错误

“语法错误:解析错误”。

我知道这个错误是由带有“.”的参数名称(sample.Svc)引起的。但我不知道在我的测试用例中使用我的服务“sample.Svc”的其他方法。一些机构帮助我以任何其他方式注入我的服务。

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

【问题讨论】:

    标签: javascript angularjs mocha.js naming karma-mocha


    【解决方案1】:

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

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

    编辑:详细说明,将您的服务称为“示例”或类似名称。

    【讨论】:

    • 感谢您的建议,但我必须遵循与我之前所说的相同的命名约定。
    猜你喜欢
    • 2023-03-22
    • 2022-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多