我有一家工厂如下:
var myFactory = function ($q) {
factory = {};
factory.newObject = function() {
return $q.when({ id: 0 }});
};
};
angular.module('myModule').factory('myFactory', myFactory);
如何使用jasmine测试newObject函数并确保返回的id为0? 这是我到目前为止所做的:
describe("My Factory Test", function () {
var testMyFactory;
beforeEach(module('myModule'));
beforeEach(inject(function (myFactory) {
testMyFactory = myFactory;
}));
it('New Entity', function ($q) {
testMyFactory.newObject().then(function(myObj) {
expect(myObj.id).toBe(0); // does not work
});
});
});
答案 0 :(得分:1)
这有效:
describe("My Factory Test", function () {
var testMyFactory, rootScope;
beforeEach(module('myModule'));
beforeEach(inject(function (myFactory, $rootScope) {
testMyFactory = myFactory;
rootScope = $rootScope;
}));
it('New Entity', function () {
var testObj;
testMyFactory.newObject().then(function(myObj) {
testObj = myObj;
});
rootScope.$apply();
expect(testObj.id).toBe(0);
});
});