【发布时间】:2014-09-18 21:58:51
【问题描述】:
我在 Angular 应用程序中使用“控制器作为”语法。现在是测试的时候了,但所有示例都是针对注入 $scope 的控制器。我如何调用“this.addItem”方法并检查它是否在 Jasmine 测试中向“this.items”添加了一个项目?
(function () {
"use strict";
angular.module('myModule', ['factoryModule'])
.controller('MyController', function (myFactory) {
this.items = [];
this.selectedItem = null;
this.addItem = function (itemType) {
var item = myFactory.create(itemType);
this.items.push(trigger);
this.selectedItem = item;
};
this.removeItem = function (item) {
this.items.splice(this.items.indexOf(item), 1);
};
});
})();
【问题讨论】:
-
在您的测试中,您可以使用
$controller服务创建一个控制器实例。并使用实例并在其上调用 additem。然后在您的期望中检查控制器实例上的 items 属性.. -
谢谢你,你的建议真的很有帮助!
标签: angularjs testing jasmine bdd