0
這是我第一次在Angular中測試,也是我第一次在新工作中測試。我試圖編寫一個測試,看看MainCtrl.lables包含的東西(或理想情況下從dataRepository獲取值) 有誰知道如何做到這一點或知道任何好的資源角度測試需要服務的控制器?我很難找到如何使用嘲笑的好信息。Angular中的單元測試
MainCtrl.js
var app = angular.module("ChartApp", ["chart.js"]);
app.controller("MainCtrl", ["$scope",
function ($scope, dataRepository) {
$scope.labels = dataRepository.labels;
$scope.data = dataRepository.data;
$scope.type = dataRepository.type;
$scope.title = dataRepository.title;
}
]);
DataSrv.js
angular.module("ChartApp")
.factory('dataRepository', function() {
return {
labels: ["Reading", "Coding", "Thinking About Coding", "Reddit", "StackOverflow"],
data: [500, 300, 300, 40, 220],
type: 'PolarArea',
title: "Angular Chart Expriment"
};
});
MainCtrlSpec.js
/// <reference path="../../Scripts/angular/angular.js" />
/// <reference path="../../Scripts/angular/angular-mocks.js" />
/// <reference path="../../Scripts/chartjs/Chart.js" />
/// <reference path="../../Scripts/angular-chart.js-master/dist/angular-chart.js" />
/// <reference path="../../Scripts/controller/main-controller.js" />
/// <reference path="../../Scripts/service/data-service.js" />
/// <reference path="../../libs/jasmine/jasmine.js" />
describe("Controller: MainCtrl", function() {
beforeEach(module("ChartApp"));
var ctrl, scope;
beforeEach(inject(function ($controller) {
scope = {};
ctrl = $controller("MainCtrl" });
}));
it("should have scope defined", function() {
expect(MainCtrl.lables).toBeDefined();
});
});