- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个类 AProvider
需要 './b.provider'
。
const BProvider = require('./b.provider');
class AProvider {
static get defaultPath() {
return `defaults/a/${BProvider.getThing()}`;
}
}
module.exports = AProvider;
b.provider.js
与 a.provider.js
相邻,看起来像
global.stuff.whatever = require('../models').get('Whatever'); // I didn't write this!
class BProvider {
static getThing() {
return 'some-computed-thing';
}
}
module.exports = BProvider;
在我的测试中,我使用 proxyquire
模拟出 ./b.provider
如下:
import { expect } from 'chai';
import proxyquire from 'proxyquire';
describe('A Provider', () => {
const Provider = proxyquire('../src/a.provider', {
'./b.provider': {
getThing: () => 'b-thing'
},
});
describe('defaultPath', () => {
it('has the expected value', () => {
expect(Provider.defaultPath).to.equal('defaults/a/b-thing')
});
});
});
然而,当我运行测试时,BProvider
仍然需要实际的 './b.provider'
而不是 stub 和 BProvider 对 global.stuff 的引用。 whatever
抛出错误。
为什么这不起作用?
最佳答案
为什么会这样,答案如下
proxyquire
在将其 stub 之前仍然需要底层代码。它这样做是为了启用调用。
解决方案只是明确禁止调用。
测试变成:
import { expect } from 'chai';
import proxyquire from 'proxyquire';
describe('A Provider', () => {
const Provider = proxyquire('../src/a.provider', {
'./b.provider': {
getThing: () => 'b-thing',
'@noCallThru': true
},
});
describe('defaultPath', () => {
it('has the expected value', () => {
expect(Provider.defaultPath).to.equal('defaults/a/b-thing')
});
});
});
运行此测试效果很好。
关于javascript - Proxyquire 没有 stub 我需要的类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42550134/
我正在尝试使用 proxyquire 来模拟快速行为,但在处理以下命令时遇到一些问题: expressProxy = () => { return { use: () =>
我尝试使用 proxyquire 来 stub child_process 模块的 spawnSync 方法,但它不起作用。我的 index.js 文件中的 console.log(gitRespon
我试图让 Proxyquire 使用简单的方法替换来工作,但我不知道我做错了什么。 我创建 lib.js module.exports = { thing: () => { c
我正在尝试使用 proxyquire 测试一个对象。该对象是一个简单的 JavaScript/TypeScript 类,具有一些依赖项。我将 proxyquire 与 sinon 一起使用来覆盖这些依
我目前正在隔离测试一个模块,使用 proxquire 覆盖这个模块的要求。 覆盖 require 的路径可以很好地使用 proxyquire。例如: var bar = require('./bar'
我无法在 Promise stub 返回的响应中获取解析值未定义 otpHelper.sendOtp = (number) => { console.log('number', n
我正在测试一个看起来像这样的脚本: import arcpy Class A: function go(x, y, z): arcpy.dothing(y,x,z['attr'
我需要模拟“mkdirp-promise” Node 模块,该模块公开如下构造函数 mkdirpPromise(dirPath) .then(() => { c
我想用 proxyquire 模拟 MongoDB 依赖通过在我的测试中这样做: var proxyquire = require('proxyquire'); var controller = pa
您好,我正在尝试使用 proxyquire 在我的 Controller 中模拟一个模型。但是出于某种原因,当尝试注入(inject)模拟时,会调用原始文件。 这就是 Controller 内部需要我
我将如何使用 proxyquire 和 sinon stub 以下模块: var email = require("emailjs").server.connect.send(); 我做了以下操作,但
如果我们有三个模块名称 A、B 和 C,那么模块 A 需要 B 和 B 需要 C:这个调用会产生什么效果? var A = proxyquire('A', {'C': mockedModule}) 模
被测模块: 'use strict'; const config = require('config'); const q = require('q'); class RedisAccess
我已经安装了 proxyquire,我的 ajax.test.tsx 文件包含以下代码,只有 2 行 import * as proxyquire from 'proxyquire'; proxyqu
我有一个类 AProvider 需要 './b.provider'。 const BProvider = require('./b.provider'); class AProvider { st
我想对以下简化模块进行单元测试: const Logger = require('logplease'); const logger = Logger.create('utils'); const t
我看过几个类似的问题,但没有一个符合我的问题。我正在尝试模拟我在其他测试中做过的构造函数,但在使用 google-auth-library 的情况下我无法让它工作 代码.js const {OAuth
我正在尝试模拟 Cassandra 的单元测试用例,我正在使用 npm Cassandra-driver 来连接/查询 cassandra。为了模拟 stub ,这是我的代码。 beforeEach(
我似乎无法从 NodeJS 核心在 fs 上 stub readFileSync。以下代码隔离了该问题。通过 Mocha 运行测试结果如下: > mocha tests/test.js S
对于 nodejs 的以下片段代码,我如何 stub send方法使用 proxyquire和sinon ,假设这属于文件 index.js ?我尝试了很多方法,但总是出错。 var emailjs
我是一名优秀的程序员,十分优秀!