2014-10-16 13 views
0

我剛從單元測試開始,所以請耐心等待。單元測試javascript函數在其他地方不知不覺中使用

以下面這個'傻'的例子。我在頁面上創建了一個要綁定函數的元素對象。我爲test.myFunction設置了一個單元測試,並確保它返回'textToReturn'。

var global = { 
    o_bindings : [ 
    { 
     object: '#element1', 
     event: 'keyup', 
     selector: '', 
     data: '', 
     theFunction: function() { 
     test.myFunction(textToReturn); 
     } 
    }, 

    // .... lots of other code .... 

    { 
     object: '#element2', 
     event: 'keyup', 
     selector: '', 
     data: '', 
     theFunction: function() { 
     test.myFunction(textToReturn); 
     } 
    } 
    ] 
}; 


$(document).ready(function() { 

    for (var i=0; i < global.o_bindings.length; i++) { 
    var o = global.o_bindings[i]; 
    $(o.object).on(o.event, o.selector, o.data, o.theFunction); 
    } 
}); 

我再講一年後,並決定#element2的應該返回每次都相同的文字,所以我硬編碼在功能和更新原始對象的一部分:

{ 
     object: '#element2', 
     event: 'keyup', 
     selector: '', 
     data: '', 
     theFunction: function() { 
     test.myFunction() 
     } 
    } 

我忘記了該函數也被用在#element1上,並相應地更新我的單元測試,以便它總是返回相同的文本。

單元測試會通過 - 但#element1不會按預期工作。

我錯過了什麼嗎?或者這會在功能測試中找到,因爲它超出了單元測試的範圍?任何建議將不勝感激。

+0

如果發生這種情況,那麼您已經忘記爲'global.o_bindings'或其他使用它的東西編寫單元測試。 – slebetman 2014-10-16 14:00:15

回答

0

我不確定我是否完全理解你的代碼,但一般來說,「單元測試」應該測試可能的最低級別函數。在這種情況下,我認爲這是test.myFunction()。所以,如果過去它接受了一個論據,然後基於這個論據返回了一些東西,而現在它沒有,那麼通過任何東西都沒關係。換句話說,如果JavaScript函數根本不使用參數,那麼傳遞它們(或不傳遞)將不會影響(因此不會影響測試的成功。)

在您的情況(上面),使用test.myFunction(textToReturn)代碼變化的不再使用的輸入其實並不重要,因爲該方法工作正常兩種方式。這就是說,讓我們假設你有一些代碼在theFunction()其中預計適當textToReturn值,那麼你會寫一個測試theFunction()它檢查這個。這是一個簡單的例子:

// sort of like your `test.myFunction()` 
function foo() { 
    return "static value"; 
} 

// the place where you remembered to change it... 
function bar() { 
    $("#" + id).text(foo()); 
} 

// the place where you forgot to change it 
function baz(id) { 
    $("#" + id).text(foo("some other value")); 
} 

對於上面的源代碼,我會測試每個函數,包括通過和那些預期會失敗的函數。以下是一些測試(會有更多):

QUnit.test("make sure foo works", function(assert) { 
    var v = foo(); 
    assert.equal(v, "static value", "The value returned by foo should be correct"); 
}); 

// I would have added this one after the change to your `test.myFunction()` 
QUnit.test("make sure foo works with input", function(assert) { 
    var v = foo("some other value"); 
    assert.equal(v, "static value", "The value returned by foo does not change with input"); 
}); 

QUnit.test("make sure bar works", function(assert) { 
    bar("theElementId"); 
    var t = $("#theElementId").text(); 
    assert.equal(t, "static value", "The value in the element should be correct"); 
}); 

/* 
// this is the ORIGINAL test for baz() 
// It would have failed after the code change 
QUnit.test("make sure baz works", function(assert) { 
    baz("theElementId"); 
    var t = $("#theElementId").text(); 
    assert.equal(t, "some other value", "The value in the element should be correct based on input to foo()"); 
}); 
*/ 

// but I would CHANGE the test above to this: 
QUnit.test("make sure baz works", function(assert) { 
    baz("theElementId"); 
    var t = $("#theElementId").text(); 
    assert.equal(t, "static value", "The value in the element should be correct"); 
}); 

注意的是,第四次試驗就失敗了,從而突出的事實,我忘了更新baz()

相關問題