我如何在茉莉花中进行这个简单的测试

时间:2014-10-26 15:13:47

标签: angularjs jasmine

我有以下测试:

describe("An Angularjs test suite",function(){
  beforeEach(module('myApp'));
  var scope,controller;
  beforeEach(inject(function($rootScope,$controller){
    scope = $rootScope.$new(),
    controller = $controller('homeController',{$scope:scope});
  }));

  it('should return true',function(){
    expect(scope.helloWorld()).toBe('hello');
  });
});
在家庭控制器中我有这个:

$scope.helloWorld = function(){
                return 'hello';
            };

测试运行有问题并说“未定义不是函数”,问题是什么?

1 个答案:

答案 0 :(得分:0)

    (function (angular) {
        // Create module
        var myApp = angular.module('myApp', []);
        myApp.controller('homeController', function($scope){
        $scope.helloWorld = function(){
            return 'hello';  
        };
    });
})(angular);



// ---SPECS-------------------------

describe('myApp', function () {
    beforeEach(module('myApp'));
    var scope,controller;
    beforeEach(inject(function($rootScope,$controller){
        scope = $rootScope.$new(),
        controller = $controller('homeController',{$scope:scope});
    }));
    it('should return true',function(){
        expect(scope.helloWorld()).toBe('hello');
    });
});

在这里创建一个TestCase

它过去了。