坚持这个。
我正在使用laravel elxir和tsify来生成我的 js。我通过 factor-bundle 运行 typescript 以将常见的 js 模块拆分为单独的文件。我认为在这种情况下这不会成为问题,因为一切都在 spec.js 中
规格
/// <reference path="../../../typings/index.d.ts" />
import "jasmine-jquery";
// @start widgets
import "./widgets/common/widget-factory/test";
工厂小部件/index.ts
export class WidgetFactory {
 .... this contains a require call to browser.service which i need to mock
}
工厂小部件/test.ts
...
import {WidgetFactory} from "./index";
const proxyRequire =  require("proxyquire");
  it("should output the factory items", ()=> {
        proxyRequire('./widgets/browser.service/index',{
            "@global": true,
   });
}
浏览器服务.ts
...
export class BrowserService implements IBrowserService{
 //details
}
在第 262 行出现错误Uncaught TypeError: require.resolve is not a function。
这是代码(是的,它超过 20,000 行)您还应该如何调试这些东西。¯_(ツ)_/¯
我用 proxyquire 看过 Stubbing。我没有屏住呼吸来回答这个问题。
编辑:06-09-2016 Proxquire 需要覆盖 WidgetFactory 类的 boot 方法中的 require 调用
在工厂小部件/index.ts 中:
boot(output = true):any {
        let required = {};
        if (this._sorted.length) {
            this._sorted.forEach((key)=> {
                if (output) {
                    console.log(`${this._path}${key}/index`);
                    // this is where is need to overide the call to require. 
                    required[key] = require(`${this._path}${key}/index`);
                }
            });
            this._sorted.forEach((key)=> {
                let dependencies = {},
                    module = this._factory[key];
                if (module.hasOwnProperty(this.dependencyKey)) {
                    module[this.dependencyKey].map((key)=> {
                        dependencies[_.camelCase(key)] = this.isService(module) ? new required[key] : key;
                    });
                }
                if (this.isTag(module)) {
                    if (output) {
                        document.addEventListener("DOMContentLoaded", ()=> {
                            riot.mount(key, dependencies);
                        });
                    }
                    //console.log(key,dependencies);
                }
                else {
                }
            })
        }
    }