2014-10-26 67 views
0

這個簡單的測試,我有以下測試:如何使茉莉

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'); 
    }); 
}); 

在HomeController的,我有這樣的:

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

測試運行錯誤,說「undefined是不是一個函數', 問題是什麼?

+1

發佈完整的錯誤消息和堆棧跟蹤以及完整的代碼。 – 2014-10-26 15:55:03

回答

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即可。

它通過了。

+0

我不明白,這個答案與我寫的答案有什麼區別,它爲什麼在這裏工作? – totothegreat 2014-10-26 18:16:29

+0

我建議你把所有的代碼發佈到jsfiddle(你最好用我的jsfiddle來做到這一點,如果它有效,建議你改變你的jasmine庫)。 – 2014-10-27 01:58:40