24

私は js ウィジェットを作成しています。最初の部分は、次のようなスクリプト幅の JavaScript を追加することです (Google アナリティクスの例):

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

ジャスミンでテストする方法(フィクスチャを使用?)?

4

3 に答える 3

23

私の仕様で HTML フィクスチャを設定するために、私はjasmine-fixtureを書きました。これを使用すると、次のようなことができます。

var $foo, $input;
beforeEach(function(){
  $foo = affix('.foo');
    # appends to the DOM <div class="foo"></div> 
  $input = $foo.affix('input[id="name"][value="Jim"]');
    # appends <input id="name" value="Jim"/> beneath the .foo div

そしてafterEach、それはあなたの後に片付けます。

DOM の状態についての期待のために、私はjasmine-jqueryを使用します。以下のような大量のマッチャーを提供します。

it('is named Jim', function(){
  expect($input).toHaveValue("Jim");
});
于 2011-11-22T13:24:20.627 に答える
4

アクションを実行してから、最初のスクリプトタグの href を次のように確認できると思います。

function addGA(){
    var ga = document.createElement('script'); 
    ga.type = 'text/javascript'; ga.async = true;
    ga.src = 
      ('https:' == document.location.protocol ? 'https://ssl' : 'http://www')
      + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(ga, s);
};

ジャスミン仕様:

 var href = 'http://www.google-analytics.com/ga.js';
 expect(document.getElementsByTagName('script')[0].href).not.toEqual(href);
 addGa();
 expect(document.getElementsByTagName('script')[0].href).toEqual(href);

ただし、より良い方法を聞くことに興味があります。Jasmine を使用して DOM をチェックするのは、いつも少し面倒に感じます。

于 2011-10-08T19:25:07.720 に答える
3

Jasmine を使用した大量の DOM テストには、 Jasmine Headless WebKitを使用できます。

于 2011-10-06T09:41:24.943 に答える