我正在尝试对包含dataService工厂的角度控制器进行单元测试。问题是我得到错误TypeError:' undefined'不是一个对象(评估' myScope.data.test')
谁能看到我做错了什么?
/// <reference path="../scripts/jasmine.js" />
/// <reference path="../scripts/angular.js" />
/// <reference path="../scripts/angular-mocks.js" />
var app = angular.module('myApp', []);
app.factory("dataService", ["$http", "$q", function ($http, $q) {
var _test = function () {
return "Hello world";
};
return {
test: _test,
};
}]);
var testController = ["$scope", "dataService",
function ($scope, dataService) {
$scope.data = dataService; // This is similar to a DAL
}];
describe('Tests my controller without mocks', function () {
var myScope;
var myDataService;
beforeEach(inject(function ($rootScope, $httpBackend, $controller) {
angular.module('myApp');
myScope = $rootScope.$new();
myDataService = $rootScope.dataService;
$controller('testController', {
$scope: myScope,
dataService: myDataService
});
}));
it('should say Hello', function () {
expect(myScope.data.test).toBe("Hello world");
});
});
答案 0 :(得分:1)
您需要使用 angular.mock.module('myApp'); 在beforeEach方法中引导您的应用。
从测试名称看,您想要使用真实服务(“没有模拟”)。为此,您应该从显式调用$ controller中省略 dataService 属性。它将连接真实实例。