【问题标题】:Durandal and Jasmine: dependency mockDurandal 和 Jasmine:依赖模拟
【发布时间】:2014-02-15 02:29:30
【问题描述】:

我在尝试模拟一些 Durandal 依赖项时遇到问题...例如,我的视图模型中有这个(在激活函数上):

routerFirstActiveFragment = router.activeInstruction().fragment.toLowerCase().split('/')[0];

我想模拟路由器插件,所以在我的测试文件中我有这个:

define(['viewmodels/testvm', 'plugins/router'], function (testvm, router) {
    describe('Module test', function () {
        it('a test', function () {
            spyOn(router, 'activeInstruction').andReturn('/get/33');
            testvm.activate();
        });
    });
});

问题是,当我这样做时,我会在运行测试时收到此消息

TypeError: Cannot read property 'toLowerCase' of undefined

那么,我做错了什么?

提前致谢!

【问题讨论】:

    标签: unit-testing mocking jasmine durandal


    【解决方案1】:

    嗯,它是这样工作的:

    spyOn(router, 'activeInstruction').andCallFake(function () {
        return {
            fragment: "/get/33"
        };
    });
    

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 1970-01-01
      • 2013-07-07
      • 2014-07-18
      • 2020-12-09
      • 1970-01-01
      • 2021-02-12
      • 2018-06-28
      • 1970-01-01
      相关资源
      最近更新 更多