1

我一直在毆打我的頭靠在牆上,試圖找出爲什麼這些測試不能使用Karma和Jasmine來測試Ionic控制器。我是新來的離子,業力單位測試,angularjs,以及這裏的其他一切。任何人都可以確定這些測試失敗的原因嗎?單元測試控制器離子框架Karma

這裏是我的控制器:

angular.module('starter.controllers') 
    .controller('TemplateCtrl', function($scope) { 
     $scope.text = 'Hello Template'; 
    }); 

這裏是我的測試:

describe('Template Controller', function(){ 
     var scope; 
     beforeEach(module('starter.controllers')); 
     beforeEach(inject(function($rootScope, $controller) { 
      scope = $rootScope.$new();       
      $controller('TemplateCtrl', {$scope: scope});  
     })); 

     // tests start here 

     it('should have scope defined', function() { 
      expect(scope).toBeDefined(); 
     }); 

     it('should have text set to Hello Template', function(){ 
      expect(scope.text).toEqual('Hello Template'); 
     }); 
    }); 

測試結果:

PhantomJS 2.1.1(Linux的0.0.0)模板的控制器應有範圍內定義失敗 預期未定義以定義。

PhantomJS 2.1.1(Linux 0.0.0)模板控制器應該將文本設置爲Hello模板失敗 TypeError:未定義不是controller-tests/template.tests.js中的對象(評估'scope.text') (第23行)

謝謝你的時間。

+1

Karma是否包含定義您的'starter.controllers'模塊以及'TemplateCtrl'控制器的文件? – Phil

+1

該應用在bootstrap上失敗('inject(...)'),因此'scope'保持不確定狀態。 Phantomjs以吞嚥錯誤而聞名,如果在控制檯中沒有顯示其他錯誤,請嘗試將其更改爲Chrome。 – estus

+1

似乎工作很好,在這裏〜http://plnkr.co/edit/NwcVhYN7fL16R1TzBFOH?p=preview – Phil

回答

0

菲爾發現問題,沒有我,甚至張貼我人緣配置文件:

Does Karma include the files that define your starter.controllers module as well as the TemplateCtrl controller?

只需添加路徑固定我的問題的業力配置文件。 謝謝菲爾!