2014-07-10 47 views
0

鑑於下面的代碼,我設法通過使用QUnit來編寫第一部分的測試,但無法測試finder.doRoutefinding。我怎樣才能'嘲笑'功能finder.do路由查找? (Mockjax不能在這裏使用,因爲沒有Ajax調用參與)測試時的問題(同時使用javascript&QUnit)

`finder.doSelectDestination = function(address) 
    { 
     finder.destination = address; 
     finder.doRoutefinding(
       finder.departure, 
       finder.destination, 
       finder.whenRouteLoaded, 
       finder.showRoute); 
    } 

test('Destination Selector', 
    function() 
    { 
     address="London"; 
     finder.doSelectDestination(address); 

     equal(pathfinder.destination,address, "Succesful Destination Selection"); 

    } 
); 

回答

0

有警告,但你可以簡單地更換功能的模擬:

var originalDoRoutefinding = finder.doRoutefinding; 
finder.doRoutefinding = function() { 
    // Mock code here. 
}; 
// Test code here. 
finder.doRoutefinding = originalDoRoutefinding; 

如果這種事情對你的作品,你可以考慮使用像Sinon.JS這樣的庫。