2017-03-10 16 views
0

ジャスミンでコードをテストしたい。私がテストしたいコードは次のとおりです。変数が定義されていない場合のジャスミンテスト

window.document.cookie = 'user_input=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT'; 

var user_input = $('.selector').val(); 

if (typeof user_input !== "undefined") { 
    var date = new Date; 

    user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' '); 
    date.setDate(date.getDate() + 1); 
    window.document.cookie = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString(); 
} 

私が持っているテストは、次のとおりです。

describe('get_user_input',() => { 
    it('should update the cookie with users input if the input is NOT undefined',() => { 
     window.document.cookie = 'test_case='; 
     var user_input = 'test'; 
     expect(window.document.cookie).toEqual('test_case=test'); 
    }); 
}); 

これはしかし、動作しませんし、任意の助けをいただければ幸いですので、私はテストに非常に新しいです!ありがとう!

答えて

1

あなたの関数は、DOMから要素を取得します。したがって、あなたのテストでは、そのような要素をDOMで作成しなければなりません。

describe('get_user_input',() => { 
    beforeEach(() => { 
     window.document.cookie = 'test_case='; 
     $("<div class='selector'>test</div>").appendTo("body"); 
     yourFunction(); 
    }); 

    it('should update the cookie with users input if the input is NOT undefined',() => { 
     expect(window.document.cookie).toEqual('test_case=test'); 
    }); 
}); 

しかし より好ましいソリューションは、あなたの関数からDOM操作を取り除くためにも、この機能を簡単にテスト可能とあまりカップリングになるだろう。

function yourFunction(user_input) { 
    window.document.cookie = 'user_input=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT'; 

    if (typeof user_input !== "undefined") { 
     var date = new Date; 

     user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' '); 
     date.setDate(date.getDate() + 1); 
     window.document.cookie = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString(); 
    } 
} 

そして、それのためのテスト:

describe('get_user_input',() => { 
    let user_input; 

    beforeEach(() => { 
     window.document.cookie = 'test_case='; 
     user_input = "test"; 
     yourFunction(user_input); 
    }); 

    it('should update the cookie with users input if the input is NOT undefined',() => { 
     expect(window.document.cookie).toEqual(`test_case=${user_input}`); 
    }); 
}); 

またはそれ以上 - の機能のうち、window.document.cookieを移動し、計算された値を返します。そして、この戻り値をwindow.document.cookieあなたのアプリの他の場所に設定します。

function yourFunction(cookie, user_input) { 
    let res = 'user_input=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT'; 

    if (typeof user_input !== "undefined") { 
     var date = new Date; 

     user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' '); 
     date.setDate(date.getDate() + 1); 
     res = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString(); 
    } 
    return res; 
} 

describe('get_user_input',() => { 
    let cookie; 
    let user_input; 
    let output; 

    beforeEach(() => { 
     cookie = 'test_case='; 
     user_input = "test"; 
     output = yourFunction(cookie, user_input); 
    }); 

    it('should update the cookie with users input if the input is NOT undefined',() => { 
     expect(output).toEqual(`test_case=${user_input}`); 
    }); 
}); 

できるだけシンプルに機能させる。独力責任は揺るがす!

+0

ありがとう!これは私を助けた! – AKr

-1

この単純なexemple Jasmin landpage参照してください:良い場所に置くコードをすることができ、これにそう

describe("A suite is just a function", function() { 
    //initialisation here 
    var a; 

    it("and so is a spec", function() { 
    //then you run the code you want to test 
    a = true; 
    //and you test 
    expect(a).toBe(true); 
    }); 
}); 

を:

describe('get_user_input',() => { 
 
    window.document.cookie = 'test_case='; 
 
    var user_input = 'test'; 
 
    it('should update the cookie with users input if the input is NOT undefined',() => { 
 

 
    if (typeof user_input !== "undefined") { 
 
     var date = new Date; 
 

 
     user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' '); 
 
     date.setDate(date.getDate() + 1); 
 
     window.document.cookie = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString(); 
 
    } 
 

 
    expect(window.document.cookie).toEqual('test_case=test'); 
 
    }); 
 
});

編集:それはとさえ渡しませんこの。 window.document.cookieにはパスが含まれ、日付は期限切れです。

describe('get_user_input',() => { 
 
     window.document.cookie = 'test_case='; 
 
     var user_input = 'test'; 
 
     it('should update the cookie with users input if the input is NOT undefined',() => { 
 

 
      if (typeof user_input !== "undefined") { 
 
      var date = new Date; 
 
      user_input = user_input.replace(/(\r\n|\n|\r)/gm, ' '); 
 
      date.setDate(date.getDate() + 1); 
 
      window.document.cookie = "user_input=" + user_input + ';path=/;expires=' + date.toGMTString(); 
 
      } 
 

 
      expect(window.document.cookie).toEqual('test_case=test;path=/;expires=' + date.toGMTString() + '); 
 
      }); 
 
     });

+0

完了回答 –

+0

申し訳ありませんが、この回答は私の問題を解決しませんでした。しかし、このスレッドで選択された答えはありました。とにかくありがとう! – AKr

関連する問題