13

ジオロケーターを呼び出す関数がありますが、この関数をテストする方法がわかりません。ジオロケーターをスパイして偽のデータを返すことを試みましたが、成功しませんでしたが、元の関数がまだ使用されているため、待たなければならず、模擬データを使用できませんでした。

// this doesn't work        
var navigator_spy = spyOn( navigator.geolocation, 'getCurrentPosition' ).andReturn( {
    coords : {
        latitude : 63,
        longitude : 143
    }
} );

これどうやってするの?

4

2 に答える 2

20

ジオロケーションコードを呼び出すと、次のようになります。

  navigator.geolocation.getCurrentPosition(onSuccess, onError);

これは、それを呼び出して関数を渡すことを意味します。

  function onSuccess(position) {
      // do something with the coordinates returned
      var myLat = position.coords.latitude;
      var myLon = position.coords.longitude;
  }

  function onError(error) {
      // do something when an error occurs
  }

したがって、値を返すjasmineを使用してスパイしたい場合は、次のように元の呼び出しの最初の引数を使用してsuccess関数を呼び出します。

  spyOn(navigator.geolocation,"getCurrentPosition").andCallFake(function() {
         var position = { coords: { latitude: 32, longitude: -96 } };
         arguments[0](position);
  });

エラーが返されたように見せたい場合は、次のように元の呼び出しの2番目の引数を使用してerror関数を呼び出します。

  spyOn(navigator.geolocation,"getCurrentPosition").andCallFake(function() {
         arguments[1](error);
  });

完全な例を表示するために編集します。

これは、Jasmineを使用してテストしている関数です。

  function GetZipcodeFromGeolocation(onSuccess, onError) {
        navigator.geolocation.getCurrentPosition(function(position) {
              // do something with the position info like call
              // an web service with an ajax call to get data
              var zipcode = CallWebServiceWithPosition(position);
              onSuccess(zipcode);
        }, function(error) {
              onError(error);
        });
  }

これはスペックファイルにあります:

  describe("Get Zipcode From Geolocation", function() {
        it("should execute the onSuccess function with valid data", function() {
              var jasmineSuccess = jasmine.createSpy();
              var jasmineError = jasmine.createSpy();

              spyOn(navigator.geolocation,"getCurrentPosition").andCallFake(function() {
                     var position = { coords: { latitude: 32.8569, longitude: -96.9628 } };
                     arguments[0](position);
              });

              GetZipcodeFromGeolocation(jasmineSuccess, jasmineError);

              waitsFor(jasmineSuccess.callCount > 0);

              runs(function() {
                    expect(jasmineSuccess).wasCalledWith('75038');
              });
        });
  });

この時点で、仕様を実行すると、Webサービスが正常に機能する場合に、指定した緯度と経度の適切な郵便番号がWebサービスから提供されたことが通知されます。

于 2012-09-06T20:01:12.040 に答える
1

beforeEachJasmineは各テストケースの後にスパイを自動的に復元するため、ブロック内にスパイを作成する必要があるかもしれません。次のようなことをした場合:

var navigator_spy = spyOn( navigator.geolocation, 'getCurrentPosition' )

it("should stub the navigator", function() {
   // your test code
});

あなたがそれをテストしたいとき、スパイはすでに復元されています。代わりにこれを使用してください:

beforeEach(function() {
    this.navigatorSpy = spyOn( navigator.geolocation, 'getCurrentPosition' )
});

it("should work now since the spy is created in beforeEach", function() {
    // test code
});
于 2012-06-19T20:18:31.263 に答える