简体   繁体   中英

How to mock specific RequireJs dependencies while unit testing

I use qUnit in the browser to unit test my pet project. 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.

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:

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?

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. Yuk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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