2016-05-04 164 views
0

我試圖測試工廠量角器和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

有人殺了我:

$provide.service('$window', function() { this.google = google; }); 

它應該有下劃線,因爲我之前

var _google = ... 

定義的變量行應該是這樣的一個: 我在這一行做了一個錯誤:

$provide.service('$window', function() { this.google = _google; });