我环顾四周,看到angular-mocks.js应该包含在html页面中以解决这个问题。我添加了这个,但量角器仍然把错误扔给我。
我对角度相对较新,所以如果我真的很感激这个问题的任何提示/建议。
以下是我的文件:
的index.html
...
<!-- scripts -->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-mocks/angular-mocks.js"></script>
<script src="bower_components/angular-local-storage/dist/angular-local-storage.js"></script>
<script src="js/app.js"></script>
<script src="js/calendar.js"></script>
<script src="js/daily-summary.js"></script>
<script src="js/add-word.js"></script>
<!-- <script src="view1/view1.js"></script> -->
<!-- <script src="view2/view2.js"></script> -->
<script src="components/version/version.js"></script>
<script src="components/version/version-directive.js"></script>
<script src="components/version/interpolate-filter.js"></script>
</body>
scenarios.js
'use strict';
/* https://github.com/angular/protractor/blob/master/docs/toc.md */
describe('my app', function() {
describe('Add Word Controller', function() {
var session;
beforeEach(inject(function($rootScope, localStorageService) { // <-- line that throws error
// set up
module('myApp');
// fetch resources
browser.get('partials/add-word.html');
// assign local storage service
session = localStorageService;
}));
afterEach(inject(function(localStorageService) {
// clear entry after each operation
localStorageService.set(Date.getDate(), '');
}));
it('should add a word to local storage when user enters a new word', function() {
// define the input elements
var word = element(by.css('.add-word input:nth-child(1)'));
var definition = element(by.css('.add-word input:nth-child(2)'));
var sentence = element(by.css('.add-word textarea'));
var addWord = element(by.css('.add-button'));
// emulate the type and click events
var wordObject = {
word: 'Animals',
definition: 'A living organism that feeds on organic matter',
sentence: 'There are a lot of animals in the jungle'
}
word.sendKeys(wordObject.word);
definition.sendKeys(wordObject.definition);
sentence.sendKeys(wordObject.sentence);
addWord.click();
// expect there to be a word
var fetchedWord = session.get(Date.getDate());
expect(fetchedWord).toEqual(wordObject);
});
});
});
添加-word.js
...
.controller('AddWordCtrl', ['$scope', 'localStorageService', function($scope, localStorageService) {
$scope.addWord = function() {
// get day of month
var dateObject = new Date();
var dayOfMonth = dateObject.getDate();
// grab word stored in (day, words) pair
var currentWords = localStorageService.get(dayOfMonth) || [];
// JSON format object
var wordObject = {
word: $scope.newWord,
definition: $scope.newDefinition,
sentence: $scope.newSentence
};
// add word to array
currentWords.push(wordObject);
// update local storage (key, value) pair
localStorageService.set(dayOfMonth, currentWords);
// update success message
$scope.successMessage = "New word added";
// lock the two inputs, one textarea and one add button
$scope.locked = true;
};
}])
;