具有Restangular服务的AngularJS单元测试控制器

时间:2016-07-12 08:30:28

标签: angularjs karma-runner karma-jasmine restangular

如何通过调用scope

来测试修改Restangular service的控制器
//services.js
/* QOD Service */
app.service('QODService',
  ['QODAPI',
    function (QODAPI) {
      return QODAPI
    }
  ]
);

//controllers.js

app.controller('QODController',
  ['$scope', 'QODService',
    function ($scope, QODService) {
      $scope.qod = null;
      QODService.one().get().then(
        function (response) {
          $scope.qod = response.qod['quote'];
        },function (error) {}
      )
    }
  ]
);

// test.js

'use strict';

describe('QOD', function () {
  var $httpBackend, QODController, response, scope = {}, QODService;

  beforeEach(module('app'));

  beforeEach(inject(function($controller, _$httpBackend_, _QODService_){
    $httpBackend = _$httpBackend_;
    QODService = _QODService_;

    response = {
      "qod": {
        "quote": 'Winning is nice if you don\'t lose your integrity in the process.',
        "author": "Arnold Horshak"
      }
    };

    $httpBackend.whenGET('/v1/qod/').respond(response);

    QODController = $controller('QODController', {
      $scope: scope, QODService: QODService
    });

  }));

  describe('QODController', function () {

    it('Should be defined', function() {
      expect(QODController).toBeDefined();
    });

    it('Should return QOD', function() {
      // Returns null ??? How do I check the response
      console.log(scope);
    });

  });
});

0 个答案:

没有答案