重用摩卡testing代码
我正在开发一个NodeJS应用程序,并使用Mocha进行unit testing。
比方说,我有两个非常相似的testing西装。 实际上这些testing是针对两个实现相同接口的类。
例如:
suit_a.js
var A = require('./a'); describe(function () { var instance; beforeEach(function () { instance = new A(); }); it(function () { assert(instance.getSomeValue() === 1); }); });
suit_b.js
var B = require('./b'); describe(function () { var instance; beforeEach(function () { instance = new B({option: "option-value"}); }); it(function () { assert(instance.getSomeValue() === 1); }); });
有没有办法删除代码重复? 有没有办法有两个不同的testing西装,使用相同的断言代码,但具有不同的configuration或类似的东西?
我现在唯一的想法是使用某种types的源代码生成,但如果可能,我想避免这种情况。
将内部函数移动到一个额外的文件并require
它。 在你的情况下,你需要额外的new A()
和new B(...)
,所以要么让它们在外部可用,要么包含它们作为require-result的参数:
var t = require('innerTestGen'); var t1 = t.create(new A()) describe(t1); var t2 = t.create(new B(...)) describe(t2);
希望有帮助?
- 确保服务器应用程序在摩卡testing开始之前运行
- 向Chai / Mocha提供应包含的部分密钥列表
- 使用伊斯坦布尔和摩卡来覆盖ES6的代码
- 用Mocha和SupertesttestingExpress错误
- 使用Mocha在NodeJS中进行OAuthstreamtesting
- 在摩卡testing预期的失败
- 用摩卡testing时,“rest()多次调用”restler错误
- Sails.js和Mocha:使用supertest:path问题,当控制器在像api \ controllers这样的coustom文件夹被移动到api \ controllers \ admin
- nodejs – testing失败,但callback被调用