用茉莉花测试角度控制器

时间:2014-03-11 07:12:19

标签: angularjs unit-testing jasmine

我使用角度几个星期后,我想对控制器和过滤器进行严格的测试。

我尝试使用茉莉花编写测试但似乎没有任何效果。然后,我试图重现官方教程中描述的测试,但没有成功。

检查出来:http://jsfiddle.net/rortelli/K2BAH/1/

提前致谢

var phonecatApp = angular.module('phonecatApp', []); 
phonecatApp.controller('PhoneListCtrl', function ($scope) {
  $scope.phones = [
    {'name': 'Nexus S',
     'snippet': 'Fast just got faster with Nexus S.',
     'age': 1},
    {'name': 'Motorola XOOM™ with Wi-Fi',
     'snippet': 'The Next, Next Generation tablet.',
     'age': 2},
    {'name': 'MOTOROLA XOOM™',
     'snippet': 'The Next, Next Generation tablet.',
     'age': 3}
  ];
});

describe('PhoneCat controllers', function() {

  describe('PhoneListCtrl', function(){
    var scope, ctrl;

    beforeEach(module('phonecatApp'));

    beforeEach(inject(function($controller) {
      scope = {};
      ctrl = $controller('PhoneListCtrl', {$scope:scope});
    }));

    it('should create "phones" model with 3 phones', function() {
      expect(scope.phones.length).toBe(3);
    });

    it('should set the default value of orderProp model', function() {
      expect(scope.orderProp).toBe('age');
    });

  });
});

2 个答案:

答案 0 :(得分:0)

加载脚本的顺序是“ReferenceError:module is not defined”

删除两个角度文件并将它们再次添加到列表底部,然后你的小提琴就可以了。

答案 1 :(得分:0)

两件事:

  1. 包括angular-mocks.js

  2. scope.orderProp在规范中不存在 所以只需将它添加到控制器,然后你将有2个成功 如下所示进行测试。

  3. http://jsfiddle.net/zNnvx/

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-mocks.js"></script>
    
    $scope.orderProp = 'age';
    
相关问题