简体   繁体   English

控制器了解服务模拟,但不了解单元测试(AngularJS,Karma-Jasmine)

[英]Controller Knows About Service Mock, but Unit Test Doesn't (AngularJS, Karma-Jasmine)

I'm adding unit tests to my AngularJS application, and running into an issue where the controller I'm testing is aware of my mocks (logs the correct mock object), but my unit tests cannot return the mock value. 我在AngularJS应用程序中添加了单元测试,并且遇到了一个问题,即正在测试的控制器知道我的模拟(记录正确的模拟对象),但是我的单元测试无法返回模拟值。 I've been stuck on this one for a while. 我已经坚持了一段时间。

Mock Service 模拟服务

angular.module('mocks.myService', [])
  .factory('myService', function() {
      var service = {
        hi : 'hi'
      };
      return service;
  });

Controller 调节器

.controller('MyCtrl', ['myService', function(myService) {
  var vm = this;

  vm.doThing = function() {
    console.log(myService);
    // Object{hi:'hi'}
  };

}]);

Unit Test 单元测试

describe('myApp.selection module', function() {
    var myCtrl, myService;

    beforeEach(function() {
      myService = module('mocks.myService');
      console.log(myService);
      // undefined
    });

    describe('controller', function(){
      it('should exist', inject(function($controller) {
         myCtrl = $controller('MyCtrl');
         expect(myCtrl).toBeDefined();
      }));

      it ('should do thing', function() {
         myCtrl.doThing();
      });

   });

});

Try this: 尝试这个:

describe('myApp.selection module', function () {
 var myCtrl, myService;

 beforeEach(module("mocks.myService"));

 beforeEach(inject(function (_myService_) {
     myService = _myService_;
     console.log(myService);
 }));

 describe('controller', function () {
     it('should exist', inject(function ($controller) {
         myCtrl = $controller('MyCtrl');
         expect(myCtrl).toBeDefined();
     }));

     it('should do thing', function () {
         myCtrl.doThing();
     });

 });

});

source: https://docs.angularjs.org/guide/unit-testing 来源: https : //docs.angularjs.org/guide/unit-testing

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

相关问题 在Karma-Jasmine中运行angularJS单元测试用例时出错 - Error while running angularJS unit test case in Karma-Jasmine 在 angularJS 的 karma-jasmine 单元测试中更改语言环境 - Changing locale in karma-jasmine unit test for angularJS angularjs单位测试业力与茉莉花模拟困境 - angularjs unit test karma with jasmine mock dilemma AngularJS:未定义$ location路径:单元测试。 卡玛 - 茉莉花 - AngularJS: $location path not defined: Unit Testing. Karma-Jasmine AngularJS,路线解析,位置更改,单元测试,业力茉莉花 - AngularJS, route resolve, location change, unit tests, karma-jasmine AngularJS控制器作为Jasmine测试并带有Promock服务 - AngularJS Controller As Jasmine test with mock service with promise 业力茉莉花单元测试中的“无法读取未定义的属性'transport'” - “Cannot read property 'transport' of undefined ” in karma-jasmine Unit Test 一项Angular Controller Karma-Jasmine测试有效,但另一项无效 - One Angular Controller Karma-Jasmine test works, but not another one 控制器方法使用AngularJs中的Karma调用Jasmine单元测试套件 - Controller methods call Jasmine unit test suite with Karma in AngularJs 使用Karma-Jasmine在Angular内以承诺测试服务 - Test a service with a promise inside in Angular with Karma-Jasmine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM