简体   繁体   English

如何使用mocha + chai在我的模块中测试名为“ sample.Svc”的角度服务

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

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

My serviceSpec.js 我的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;
    });
});

While running my serviceSpec.js (using Karma) 在运行serviceSpec.js (使用Karma)
I am getting error like 我收到类似的错误

"SyntaxError: Parse error". “ SyntaxError:解析错误”。

I know this error caused due to the argument name(sample.Svc) with ' . 我知道此错误是由于带有'的参数名称(sample.Svc)引起的. '. '。 But I dont know other ways to use my service " sample.Svc " in my testcases. 但是我不知道在我的测试用例中使用我的服务“ sample.Svc ”的其他方法。 Some body help me to inject my service in any other way. 某些身体可以帮助我以其他任何方式注入服务。

Services are named by using dot by others, I have permission to change them. 服务是由其他人使用点命名的,我有权更改它们。 So I need some solution with same naming structure. 所以我需要一些具有相同命名结构的解决方案。
Thanks in advance. 提前致谢。

Try using best practices for naming conventions. 尝试使用最佳做法来命名约定。 You will save a lot of time and headaches 您将节省很多时间和头痛

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

Edit: to elaborate, call your service "Sample" or something like that. 编辑:详细说明,将您的服务称为“示例”或类似的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM