如何使用mocha + chai

时间:2016-02-15 09:51:31

标签: javascript angularjs mocha naming karma-mocha

我的服务文件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)
我收到了像

这样的错误
  

“SyntaxError:Parse error”。

我知道由于参数名称(sample.Svc)带有“.”而导致此错误。但我不知道在我的测试用例中使用我的服务“sample.Svc”的其他方法。有些身体帮助我以任何其他方式注射我的服务。

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

1 个答案:

答案 0 :(得分:0)

尝试使用命名约定的最佳做法。你会节省很多时间和头痛

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

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