如何测试这个 angular 工厂
How to test this angular factory
我正在尝试使用量角器和 angular.mocks
测试工厂
这里是工厂:
angular.module('googlemarker', [])
.factory('newMarker', newMarkerFactory);
newMarkerFactory.$inject = ['$window'];
function newMarkerFactory($window) {
function newMarker(markerOpts) {
var position;
if (markerOpts.geo) {
position = new $window.google.maps.LatLng(
markerOpts.geo.lat || markerOpts.geo.latitude,
markerOpts.geo.lng || markerOpts.geo.longitude
);
}
var markerOptions = _.assign({
position: markerOpts.position || position,
icon: '/static/img/markers/pointer_basic.png'
}, markerOpts);
$window.google.maps.Marker.call(this, markerOptions);
}
/* this is for proper inheritance */
function helperConstructor() {}
helperConstructor.prototype = $window.google.maps.Marker.prototype;
newMarker.prototype = new helperConstructor();
newMarker.prototype.constructor = newMarker;
return newMarker;
}
然后我有这个测试:
(function() {
fdescribe('googlemarker tests', function () {
'use strict';
var _google = {
maps: {
Marker: function (markerOpitons) {
this.position = markerOpitons.position;
},
LatLng: function (lat, lng) {
this._lat = lat;
this._lng = lng;
}
}
};
_google.maps.LatLng.prototype.lat = function() { return this._lat; };
_google.maps.LatLng.prototype.lng = function() { return this._lng; };
beforeEach(module('googlemarker'));
beforeEach(module(function($provide) {
$provide.service('$window', function() { this.google = google; });
}));
it("googlemarker transform geo in google.maps.LatLng", inject(function (newMarker) {
var opts = {
geo: {
latitude: 2,
longitude: 2
}
};
var marker = new newMarker(opts);
expect(marker.position.lat()).toEqual(opts.geo.latitude);
}));
});
})();
但是这不起作用,它说我在线未定义:
"function newMarker" 说那是未定义的。有人可以帮助我吗?
有人杀了我:
我在这一行犯了一个错误:
$provide.service('$window', function() { this.google = google; });
下划线应该是我之前定义的变量
var _google = ...
这一行应该是这样的:
$provide.service('$window', function() { this.google = _google; });
我正在尝试使用量角器和 angular.mocks
测试工厂这里是工厂:
angular.module('googlemarker', [])
.factory('newMarker', newMarkerFactory);
newMarkerFactory.$inject = ['$window'];
function newMarkerFactory($window) {
function newMarker(markerOpts) {
var position;
if (markerOpts.geo) {
position = new $window.google.maps.LatLng(
markerOpts.geo.lat || markerOpts.geo.latitude,
markerOpts.geo.lng || markerOpts.geo.longitude
);
}
var markerOptions = _.assign({
position: markerOpts.position || position,
icon: '/static/img/markers/pointer_basic.png'
}, markerOpts);
$window.google.maps.Marker.call(this, markerOptions);
}
/* this is for proper inheritance */
function helperConstructor() {}
helperConstructor.prototype = $window.google.maps.Marker.prototype;
newMarker.prototype = new helperConstructor();
newMarker.prototype.constructor = newMarker;
return newMarker;
}
然后我有这个测试:
(function() {
fdescribe('googlemarker tests', function () {
'use strict';
var _google = {
maps: {
Marker: function (markerOpitons) {
this.position = markerOpitons.position;
},
LatLng: function (lat, lng) {
this._lat = lat;
this._lng = lng;
}
}
};
_google.maps.LatLng.prototype.lat = function() { return this._lat; };
_google.maps.LatLng.prototype.lng = function() { return this._lng; };
beforeEach(module('googlemarker'));
beforeEach(module(function($provide) {
$provide.service('$window', function() { this.google = google; });
}));
it("googlemarker transform geo in google.maps.LatLng", inject(function (newMarker) {
var opts = {
geo: {
latitude: 2,
longitude: 2
}
};
var marker = new newMarker(opts);
expect(marker.position.lat()).toEqual(opts.geo.latitude);
}));
});
})();
但是这不起作用,它说我在线未定义: "function newMarker" 说那是未定义的。有人可以帮助我吗?
有人杀了我: 我在这一行犯了一个错误:
$provide.service('$window', function() { this.google = google; });
下划线应该是我之前定义的变量
var _google = ...
这一行应该是这样的:
$provide.service('$window', function() { this.google = _google; });