- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试针对使用原型(prototype)模式创建对象的 Node 模块运行一些 Mocha 测试。代码本身运行得很好,但测试却不行。
这是我要测试的代码:
"use strict";
const fs = require("fs"),
request = require("request"),
EventEmitter = require("events").EventEmitter,
util = require("util");
const FileController = function(url, path) {
EventEmitter.call(this);
this.url = url;
this.path = path;
};
FileController.prototype.downloadFile = function() {
if (this.url) {
let file = fs.createWriteStream(this.path);
file.on("finish", function() {
file.close(function() {
this.emit("downloaded");
});
}).on("error", function(err) {
this.handleDownloadError(err, "file-error");
});
// Download the file.
request.get(this.url)
.on("response", function(res) {
if (res.statusCode == 200) {
res.pipe(file);
} else {
fs.unlink(this.path);
}
this.emit("stream", res);
})
.on("error", function(err) {
this.handleDownloadError(err, "request-error");
});
}
};
FileController.prototype.handleDownloadError = function(err, type) {
fs.unlink(this.path);
this.emit(type, err);
};
util.inherits(FileController, EventEmitter);
module.exports = FileController;
然后这是我实例化对象的测试代码的相关部分:
beforeEach(function() {
let url = "http://example.com/logo.png",
path = config.downloadPath + "/cdf42c077fe6037681ae3c003550c2c5";
fileController = new FileController(url, path);
// Outputs 'undefined'.
console.log(fileController.downloadFile);
});
当我调用 new FileController(url, path)
时,它没有附加我附加到原型(prototype)的 downloadFile
方法。相反,尝试调用该函数会给出 fileController.downloadFile 不是函数
。
对于问题出在哪里有什么想法吗?
谢谢!
最佳答案
这与 Mocha 无关。在定义自己的原型(prototype)方法之前,您需要继承。
来自文档
Inherit the prototype methods from one constructor into another. The prototype of constructor will be set to a new object created from superConstructor.
util.inherits(FileController, EventEmitter);
FileController.prototype.downloadFile = function() {}
UPD至于新版本的node.现在设置 ctor.prototype
prototype所以顺序不再重要。
exports.inherits = function(ctor, superCtor) {
//...Args check
ctor.super_ = superCtor;
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
};
关于javascript - Mocha 返回原型(prototype)方法未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39040745/
我正在使用 mocha 和 chai 进行 TDD 以测试一些 NodeJS 脚本,每次我想再次运行测试时都必须运行“mocha”。 有没有办法设置 mocha 和 chai 在我保存源文件后立即运行
如果任何测试失败,我正在尝试配置 mocha 以重试整个套件。 我导航到一个 URL,然后填充一个表单并提交,然后用户被重定向,如果找到某个元素,则最后一次测试通过。 如果找不到该元素,我需要再次导航
我想知道是否有办法让 mocha 列出它将执行的所有测试。当我使用 mocha --help 列出它们时,我没有看到任何合理的选项;有几个报告者,但似乎没有一个旨在列出将要处理的文件(或命名将要运行的
before()、after()、beforeEach()、afterEach() 等 mocha Hook 无法正常工作。 only 方法也不起作用。 beforeEach 都没有被调用。我得到一个
我已经看到很多关于此的问题,但所有修复都不适用于我的代码。 我做错了什么?我的 beforeEach 没有被执行。 这是单元测试代码: var assert = require('assert');
如何在 mocha 测试之间共享资源(例如连接)? cookies.test.js: describe('Cookies', function() { it('setCookie()', func
我正在尝试以编程方式运行 mocha。 runner的代码如下: var Mocha = require("mocha"); var mocha = new Mocha(); mocha.report
我正在遵循此 tutorial 的第 1 步 我有以下文件夹结构: ├── lib │ ├── json │ │ ├── messages.json │ │ └── testMessages.json
我希望能够扩展 mocha 测试结果并从可用的 mocha 对象中收听它们。首先,我正在寻找“通过”结果。 看起来他们可能是从套件中订阅的,但我不确定如何...... 我尝试了以下我认为会听到我所有测
我正在运行此测试(简化),该测试按预期失败了……但是,尽管失败了,但它仍要等待20秒的全部超时时间。如何使它立即失败? it("should fail before timeout", functio
我是Java语言世界的新手,主要涉足OOP。我试图在网上查找Karma和 Mocha 之间的明显区别,但徒劳无功。我知道Karma是一个测试运行器,而Mocha是一个单元测试框架,但是Mocha也有自
我有一段代码,其中某些测试在 CI 环境中总是会失败。我想根据环境条件禁用它们。 如何在运行时执行期间以编程方式跳过 mocha 中的测试? 最佳答案 您可以通过在describe或it block
我想使用 chai 应该断言检查我的响应对象是否包含提到的属性。 下面是我的代码片段: chai.request(app) .post('/api/signup') .
例如,默认情况下,背景颜色为绿色或红色。我想将绿色/红色作为前景,将背景作为我的默认颜色(白色)。 因为在浅色终端配色方案上很难看到任何东西,因为前景是黑色的,而背景会变成红色或绿色。 最佳答案 在
有没有办法在 mocha 记者中获取当前测试的文件名? 我在基础和示例中找不到任何内容。 最佳答案 实际上,文件名在 中传递给了 Suite文件 Mocha 中的字段从 this 开始拉取请求。只是现
在我走向 TDD 的过程中,我使用了 Mocha、chai 和 sinon。 那里肯定有一个学习曲线。 我的目标是编写一个测试来验证 method4 是否已执行。我如何做到这一点? //MyData.
我正在使用mocha和chai作为断言。 我在规范中有几个主张: Exp1.should.be.true Exp2.should.be.true Exp3.should.be.true 如果其中之一失
根据 Mocha 文档,“Mocha 测试串行运行”这意味着按照它们的定义顺序。 我的问题是:是什么让 异步 (带有完成回调)测试不同于 同步 ? 最佳答案 您通过传递给 it 来告诉 Mocha 测
我正在尝试将 mocha 绑定(bind)写入 PureScript 并且对 Control.Monad.Eff 完全感到困惑 describe(function(){ //do stuff }
我对 mocha 框架有点陌生。此代码应该抛出异常,但不会。 (为简单起见,将所有代码放入测试中) describe("Test", function() { it("this should
我是一名优秀的程序员,十分优秀!