0
我正在使用angular js 1.5。我有一个组件有一个电影列表(数组),并期望使用指令(电影项目)呈现电影列表。angularjs测试组件中的指令
我想单元测试这个组件,并确保它已经呈现与电影列表数组长度匹配的电影。
电影项目指令希望收集来自用户的输入,但我只是简化它。
我该如何测试?
电影列表组件
(function() {
"use strict";
var module = angular.module("psMovies");
function controller() {
var model = this;
model.movies = [];
model.$onInit = function() {
model.movies = [{"id": 1,"title": "Star Wars"},{"id": 2,"title": "Star Trek"}];
};
}
module.component("movieList", {
templateUrl: "movie-list.component.html",
controllerAs: "model",
controller: [ controller]
});
}());
电影list.component HTML
<div ng-repeat="movie in model.movies">
<movie-item item="movie"> </movie-item>
</div>
电影项组件
angular.module('psMovies')
.directive('movieItem', function() {
"use strict";
return {
templateUrl: 'movie-item.component.html',
restrict: 'EA',
scope: {
item: '=',
},
link: function(scope) {
}
};
});
电影项目HTML
<div> {{model.id}} - {{model.title}}</div>
我的单元测试
describe("The movieList component", function() {
beforeEach(module("psMovies"));
var moviesList;
beforeEach(inject(function ($componentController) {
moviesList = $componentController("movieList",{
$scope: {}
});
}));
it("can be created", function() {
expect(moviesList).toBeDefined();
expect(moviesList.$onInit).toBeDefined();
});
});
的问题是不够清晰。哪一个是“指令”,哪一个是“组件”。您发布的代码中只有'movieItem' *组件*。 – estus
@estus,我解决了这个问题。 – dream123
尝试注入'beforeEach(注入(函数($ componentController,_ $ rootScope_)''并将其分配给'$ scope = _ $ rootScope.new()_; –