单元测试视图模型并模拟依赖项

Luc*_*cas 5 knockout.js durandal hottowel

我在javascript测试世界中很新,我在我的hottowel应用程序中实现一些问题.我在网上找到的大多数例子都没有测试amd/require,而关于amd/require的例子没有显示其他一些东西.

我试图通过传递模拟服务来测试我的虚拟现实,让我们说...

视图模型:

define(['services/dataService'], function (dataService) { function activate() { dataService.returnSomething(); } });
Run Code Online (Sandbox Code Playgroud)

有人能指出我正确的方向(理想情况下是一个具体的例子)如何实现这一目标?任何测试框架和模拟库都可以.

谢谢

Ken*_*eth 3

我目前正在使用jasmine对我的视图模型进行单元测试。

使用 Jasmine,您将拥有一个执行所有 ViewModel 的 HTML 页面。它允许您模拟函数。我链接到的页面包含有关 Jasmine 功能的完整描述。

例子:

var dataService = Require("services/dataService");
spyOn(dataService , 'returnSomething').andReturn("something");
// execute the system under test here
expect(dataService.returnSomething).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)