Ber*_*Cim 4 javascript unit-testing mocking
我正在使用JsTestDriver和一些Jack(仅在需要时).有没有人知道如何验证在单元测试期间是否调用了javascript函数?
例如
function MainFunction()
{
var someElement = ''; // or = some other type
anotherFunction(someElement);
}
Run Code Online (Sandbox Code Playgroud)
并在测试代码中:
Test.prototype.test_mainFunction()
{
MainFunction();
// TODO how to verify anotherFunction(someElement) (and its logic) has been called?
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
JavaScript是一种非常强大的语言,您可以在运行时更改行为.
您可以在测试期间用您自己的替换anotherFunction并验证它已被调用:
Test.prototype.test_mainFunction()
{
// Arrange
var hasBeenCalled = false;
var old = anotherFunction;
anotherFunction = function() {
old();
hasBeenCalled = true;
};
// Act
MainFunction();
// Assert (with JsUnit)
assertEquals("Should be called", true, hasBeenCalled);
// TearDown
anotherFunction = old;
}
Run Code Online (Sandbox Code Playgroud)
注意:您应该知道此测试会修改全局函数,如果它失败,它可能无法始终恢复它.
你最好选择JsMock.
但是为了使用它,你需要将函数分开并将它们放入对象中,这样你根本就不会有任何全局数据.
| 归档时间: |
|
| 查看次数: |
423 次 |
| 最近记录: |