简体   繁体   English

单元测试时如何模拟特定的RequireJs依赖关系

[英]How to mock specific RequireJs dependencies while unit testing

I use qUnit in the browser to unit test my pet project. 我在浏览器中使用qUnit对我的宠物项目进行单元测试。 The module I am unit testing can look like: 我正在进行单元测试的模块如下所示:

// Filename: views/base.js

define(['../manager'], function(Manager){

  return Backbone.View.extend({

   //...

});

I want to mock out the dependency "../manager" (manager.js) I tried using testr.js and Squire.js both of which suggest to modify the path to the config for paths. 我想模拟依赖项"../manager" ),我尝试使用testr.jsSquire.js两者都建议将路径修改为配置的路径。

Example: 例:

testr.config({
    root: '../',
    baseUrl: 'src',
    stubUrl: 'tests/mocks',
    ignore: ['jquery', 'underscore', 'backbone'],
    whitelist: ['tests/unit']
});

The problem I see after I run this code is that the module I am testing (base.js) is also mocked out as well as unit test module because they are included as dependencies in unit-test runner: 运行此代码后,我看到的问题是我正在测试的模块(base.js)以及单元测试模块也被模拟了,因为它们作为依赖项包含在单元测试运行器中:

define([
  'tests/unit/test-unit',
  'tests/unit/views/base-view-unit',
], function(Test, BaseViewTest){

// ...

});

Console output: 控制台输出:

http://localhost:63342/project/tests/mocks/js/views/base.stub.js 404 (Not Found)
http://localhost:63342/project/tests/mocks/tests/unit/views/base-view-unit.stub.js 404 (Not Found)

I could add those two files into white list but that won't resolve my problem. 我可以将这两个文件添加到白名单中,但不能解决我的问题。

I guess my question is: how to mock out the deps of the file I am testing right now but not the file itself when it is included into the unit test runner? 我想我的问题是:如何模拟当前正在测试的文件的dep,而不模拟包含在单元测试运行器中的文件本身?

Ok I've found the possible answer in this post saying that I need to use the separate config.js to Load Mock Dependencies From Different Paths. 好的,我在这篇文章中找到了可能的答案,说我需要使用单独的config.js从不同的路径加载模拟依赖项。 Yuk. Yu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM