var app= angular.module('app', []);
下面是我廠方法將從sample.json角JS模擬JSON單元測試
app.factory('factoryGetJSONFile', function($http) {
return {
getMyData: function(done) {
$http.get('mock/sample.json')
.success(function(data) {
done(data);
})
.error(function(error) {
alert('An error occured whilst trying to retrieve your data');
});
}
}
});
得到下面的數據是我的控制器。我能夠訪問我的控制器
app.controller('homeController', ['$scope', 'factoryGetJSONFile', function ($scope, factoryGetJSONFile) {
factoryGetJSONFile.getMyData(function (data) {
$scope.name = data.projectDetails.name;
$scope.duration = data.projectDetails.duration;
console.log($scope.name+ " and duration is " + $scope.duration);
});
}]);
服務數據下面是我sample.json
{
"status": 200,
"projectDetails": {
"name": "Project Name",
"duration": "4 Months",
"business_place": "Dummy address"
}
}
如何編寫單元測試的情況下對上述獲得服務。我想在我的測試用例中測試projectDetails.name。
你可以在這裏找到[鏈接](http://jsfiddle.net/seshuggina/5u7L8mou/2 /) http://jsfiddle.net/seshuggina/5u7L8mou/2/ – Seshu