摩卡/茉莉花单元测试`TypeError:undefined不是函数`...不说在哪里

时间:2014-09-18 22:59:25

标签: javascript angularjs jasmine mocha

我是Angular / Javascript测试新手,这是我第一次使用Mocha进行单元测试。我正在做的是在输入框中给出NodeInVip-49-1的值,并期望下拉类型至少具有NodeInVip。

我遇到了一个奇怪的问题......我得到了TypeError: undefined is not a function。它并没有真正说明在哪里,所以我不知道什么/哪里有什么不对。


spec.js:

describe('Controller: BindNodeCtrl', function() {
    var scope, NodeService, prepareInputEl, changeInputValueTo;

    beforeEach(function() {
      var mockRestService = {};
      module('main', function($provide) {
        $provide.value('NodeService', mockRestService);
      });

      inject(function($q) {
        mockRestService.data = [
          {"stats": {"conn_max": 0, "pkts_out": 0, "bytes_in": 0, "pkts_in": 0, "conn_tot": 0, "conn_cur": 0, "bytes_out": 0}, "health_strategy": {"http_body_pattern": "", "strategy": "unknown", "ssl": false, "http_code_ok": [], "path": "", "method": "none", "port": -1}, "links": {"self": {"href": "https://foo.net/loadbalancers/534583/nodes/NodeInVip-49-1%3A.2.1.49%3A9496", "rel": "self"}, "rel": {"href": "https://loadbalancers/534583/nodes", "rel": "up"}}, "runtime_state": "ENABLED", "label": "NodeInVip-49-1", "port_name": "9496", "limit": -1, "admin_state": "DISABLED", "address": "1.2.1.49", "port_number": 9496, "id": "NodeInVip-49-1:1.2.1.49:9496", "vendor_extensions": {}} ]

        mockRestService.getNodes = function() {
          var defer = $q.defer();
          defer.resolve(this.data);
          return defer.promise;
        };
      });
    });

    beforeEach(inject(function($controller, $compile, $rootScope, _NodeService_) {
      scope = $rootScope.$new();
      NodeService = _NodeService_;

      $controller('BindNodeCtrl', { 
        $scope: scope,
        NodeService: NodeService
      });

      scope.$digest();

      prepareInputEl = function (inputTpl) {
        var el = $compile(angular.element(inputTpl))(scope);
        scope.$digest();
        return el;
      };

      var findInput = function (element) {
        return element.find('input');
      };

      var changeInputValueTo = function (element, value) {
        var inputEl = findInput(element);
        inputEl.val(value);
        inputEl.trigger($sniffer.hasEvent('input') ? 'input' : 'change');
        scope.$digest();

    };
  }));

    it("should show typeahead NodeInVip-49-1", function () {
      var element = prepareInputEl('<input ng-model="$parent.node.id" typeahead-min-length="1" typeahead="dropDownItem as dropDownItem.label for dropDownItem in nodes | filter:{label:$viewValue}>');
      changeInputValueTo(element, 'NodeInVip-49-1')
      expect(element).to.contain('NodeInVip');
    });
});

控制器:

angular.module("main.loadbalancer").controller "BindNodeCtrl", ($scope,
  VipService,
  NodeService,
  Account,
  StatusTrackerService
) ->
  NodeService.getNodes().then (nodes) -> $scope.nodes = nodes

  $scope.modalEventConfig = StatusTrackerService.modalEventConfig
  $scope.close = ->
    StatusTrackerService.modalEventConfig.close()

Controller: BindNodeCtrl should show typeahead NodeInVip-49-1.
    ✘ TypeError: undefined is not a function
        at null.<anonymous> (http://10.14.213.161:7357/generated/js/spec.js:2666:7)
        at jasmine.Block.execute (http://10.14.213.161:7357/testem/jasmine.js:1064:17)
        at jasmine.Queue.next_ (http://10.14.213.161:7357/testem/jasmine.js:2096:31)
        at http://10.14.213.161:7357/testem/jasmine.js:2086:18

0 个答案:

没有答案