2012-09-20 34 views
1

我終於成功地完成一個很簡單的端到端測試,即進樣器和模擬AngularJS服務

scenarios.js:

describe('My app', function() { 
    beforeEach(function() { 
     browser().navigateTo('/'); 
    }); 

    describe('On Load', function() { 
     it('Textbox should be blank', function() { 
      expect(input('textbox').val()).toBe(''); 
     }); 
    }); 
}); 

而且我runner.html看起來像這樣:

<head> 
    <title>End2end Test Runner</title> 
    <script src="/js/libs/angular-scenario.js" ng-autotest></script> 
    <script src="/js/libs/angular-mocks.js"></script> 
    <script src="/js/test/e2e/scenarios.js"></script> 
</head> 

這啓動我的網站,導航,並檢查輸入是空白的。高手。

然而我想現在添加此測試,擴展了我到目前爲止有:

describe('Click on Add', function() { 
    it('will add new item if valid', function() { 
     input('newItemName').enter('Test Item'); 
     element('#add').click(); 
     expect(input('newItemName').val()).toBe(''); 
     expect(repeater('#items li').count()).toEqual(1); 
    }); 
}); 

但在此之前我運行這個測試我想嘲笑存儲服務我的應用程序依賴。

這裏是我的控制器定義

myapp.controller('MyController', ['$scope', '$routeParams', '$filter', 'storage', 
    function ($scope, $routeParams, $filter, storage) { 
     . . . . 
    } 
]); 

這也是造成我的問題(我認爲),因爲我想要做這樣的事情:

describe('Controller Unit Tests', function(){ 
    var ctrl; 

    beforeEach(function(){ 
     ctrl = new MyController(); 
    }); 

    it('should ....', function() { 
     expect(true).toBe(true); 
    }); 
}); 

但不能因爲我宣佈我的控制器的方式。我之所以宣佈我的控制器是爲了縮小目的...

有沒有人有關於如何解決這個問題的建議?

+0

將是巨大的添加testatcular的配置文件,尤其是你可能已經指定了'urlRoot'除'「/」',爲了支持你的'瀏覽器()。的NavigateTo ('/')' –

回答

3

嘗試

function MyController($scope, $routeParams, $filter, storage) {} 
MyCtrl1.$inject = ['$scope', '$routeParams', '$filter', 'storage']; 
+0

我發現這個 - http://docs.angularjs.org/tutorial/step_05-關於Angular Tutorial(我從來沒有讀過!),它有答案和原因,這是正確的。 「關於縮小的說明」... – Greg