- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在运行 Mocha UT Framework + supertest + chai。
我运行了以下函数:
function Test1(inputObj) {
return function(done) {
...
require
.post('...')
.expect(302)
.end(function(err, res) {
if (err) return done(err);
inputObj.id = 'someIdFromResponse';
console.log('after update: ', inputObj); // inputObj includes id
done();
}
}
function Test2(inputObj) {
return function(done) {
console.log('Test2.inputObj: ', inputObj); // no id is printed!
done();
}
}
运行以下 Mocha 步骤:
var globalInputObj = { name: 'test' };
describe('test suite 1\n', function() {
it('should add id to input obj', Test1(globalInputObj));
it('only prints the globalInputObj', Test2(globalInputObj));
}
...
// this line runs after the describe function, guaranteed!
console.log('globalInputObj: ', globalInputObj); // no id field in object
globalInputObject 在测试函数运行后不会更新,尽管它是通过引用传递的。
我在这里缺少什么?有什么想法可以解决这个问题吗?
最佳答案
这一行
console.log('globalInputObj: ', globalInputObj); // no id field in object
您在 describe
调用执行任何测试运行之前放置的内容。因此,您没有得到想要的结果也就不足为奇了。在对问题的编辑中,您会显示一系列 it
调用,其中一个 it
调用取决于前一个调用。这不是设置测试的正确方法。使用 Mocha 的正确方法是确保每个测试独立于另一个测试。任何初始化代码都应该位于 before
或 beforeEach
Hook 中。根据您更新的问题,您可以像这样构建测试:
describe('test suite 1\n', function() {
// Initialize a test object.
var inputObj = { name: 'test' };
// The before hook gets
before(function (done) {
...
require
.post('...')
.expect(302)
.end(function(err, res) {
if (err) return done(err);
inputObj.id = 'someIdFromResponse';
done();
}
});
it('should add id to input obj', function () {
assert.equal(inputObj.id, 'someIdFromResponse');
});
it('only prints the globalInputObj', function () {
console.log('inputObj: ', inputObj);
});
}
我使用上面的 assert.equal
来执行断言。您可以使用任何您喜欢的断言库。
关于node.js - Mocha 正在复制对象的值,因此无法更新它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23477708/
我正在使用 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
我是一名优秀的程序员,十分优秀!