我试图用量角器和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);
}));
});
})();
然而,这不起作用,它说我在线未定义: "功能newMarker"说那是不确定的。有人可以帮帮我吗?
答案 0 :(得分:0)
有人杀了我: 我在这一行上犯了一个错误:
$provide.service('$window', function() { this.google = google; });
它应该有下划线作为我之前定义的变量
var _google = ...
这一行应该是这样的:
$provide.service('$window', function() { this.google = _google; });