目前伊斯坦布尔只为我的测试中使用的文件生成了覆盖范围,这是可以的,但似乎无法实现覆盖的目的.
我没有Istanbul配置,并通过npm test以下脚本字符串调用它:
$ istanbul cover _mocha -- -R dot --check-leaks --recursive test/
有没有办法为我的所有源代码生成报道?
我正在尝试向我的节点JS服务器发出请求,该服务器接受post/put调用.我试图通过chai通过post调用发送的参数在服务器上不可见(req.body.myparam).
我已尝试过以下帖子请求但结果没有结果: -
var host = "http://localhost:3000";
var path = "/myPath";
chai.request(host).post(path).field('myparam' , 'test').end(function(error, response, body) {
Run Code Online (Sandbox Code Playgroud)
和
chai.request(host).post(path).send({'myparam' : 'test'}).end(function(error, response, body) {
Run Code Online (Sandbox Code Playgroud)
节点JS代码如下: -
app.put ('/mypath', function(req, res){ //Handling post request to create league
createDoc (req, res);
})
app.post ('/mypath', function(req, res){ //Handling post request to create league
createDoc (req, res);
})
var createDoc = function (req, res) {
var myparam = req.body.myparam; //league id to create new league
if (!myparam) {
res.status(400).json({error : 'myparam is missing'});
return; …Run Code Online (Sandbox Code Playgroud) 我正在将结果的测试设置到REST端点,该端点返回一组Mongo数据库对象.
[{_id: 5, title: 'Blah', owner: 'Ted', description: 'something'...},
{_id: 70, title: 'GGG', owner: 'Ted', description: 'something'...}...]
Run Code Online (Sandbox Code Playgroud)
我希望我的测试验证的是,在返回数组中,它包含应该返回的特定标题.我没有用Chai/Chai-Things做什么似乎都有用.事情是这样res.body.savedResults.should.include.something.that.equals({title: 'Blah'})的错误了,我假设,因为记录对象包含其他键和值除了刚才称号.
有没有办法让它做我想要的?我只需要验证标题是否在数组中,而不关心其他数据可能是什么(IE _id).
谢谢
我从Node中的测试开始。使用mocha,chai和nock(拦截外部HTTP api调用)。
我已经编写了3个测试,所有这些都通过了,但是,当我添加了第3个测试时,mocha在运行测试后停止退出,没有错误或任何错误的迹象。
如果我对第三项测试发表评论,那么摩卡咖啡就可以退出。
这是导致“问题”的测试:
describe('tokenizer.processFile(req, \'tokenize\')', () => {
it('should tokenize a file', async () => {
req = {
file: {
originalname: 'randomcards.txt',
buffer: cardsFile_buffer
},
user: {
displayName: user
}
};
expect(Buffer.from(await tokenizer.processFile(req, 'tokenize'))).to.deep.equal(tokensFile_buffer);
});
});
Run Code Online (Sandbox Code Playgroud)
再次,那个测试是通过,这使我感到困惑。
这是tokenizer.processFile的代码:
processFile: function(req, whatTo){
combinedLogger.info(`Request to ${whatTo} ${req.file.originalname} received. Made by: ${req.user.displayName}`);
return new Promise(function(resolve, reject){
const lines = [], responses = [];
const lineReader = require('readline').createInterface({
input: require('streamifier').createReadStream(req.file.buffer)
});
lineReader.on('line', line => {
lines.push(line);
});
lineReader.on('close', async () => …Run Code Online (Sandbox Code Playgroud) 我正在使用 TypeScript 创建 API 测试框架,但我对它还很陌生。
我创建了这个帮助文件assertions.ts
import * as chai from 'chai';
interface response {
status: number
body: object
}
const assertResponseStatusCode =
(response:response, statusCode:number) =>
chai.expect(response.status).to.deep.equals(statusCode);
const assertSuccessResponseStatusCode = (response:response) =>
assertResponseStatusCode(response, 201)
export = {assertSuccessResponseStatusCode}
Run Code Online (Sandbox Code Playgroud)
然后我开始在我的规范文件中使用它们,如下所示
import * as auth from '../Helpers/auth';
import * as assert from '../Helpers/assertions';
import { user } from '../Samples/user';
describe('login', () => {
it('should return access token with valid credentials', async() => {
const response = await auth.login(user);
console.log(response.body);
assert.assertSuccessResponseStatusCode(response);
});
}); …Run Code Online (Sandbox Code Playgroud) 我的开发过程的一部分涉及使用 Mocha 和 Chai 测试。我的测试文件夹中有一个页面,用于加载所有代码和测试,我可以启动一个运行简单本地服务器的本地节点脚本,在 上访问该页面http://localhost:8080/blahblahblah.html,并查看测试结果。
GitHub 最近发布了 Codespaces,我注册了测试版。如果我开始在 Codespace 中进行开发,我知道那里有一个终端。如果我在该 Codespace 中运行测试服务器,我将如何查看测试结果?是否可以从外部连接到容器中的服务器?什么可以替代我上面显示的 URL?
有没有办法在Mocha中测试函数是否调用特定方法或外部函数?
我正在使用Mocha和Chai,但我对任何其他断言库都开放.
好的,所以使用sinon测试是否正在调用methid是非常容易的.我不确定测试是否正在调用外部函数.所以我更新了这些例子来代表一些更"现实世界"的东西.我工作的一个节点的应用程序,所以foo.js和bar.js是两个模块.
var bar = require('bar');
var xyz = function () {};
var Foo = module.exports = function () {
this.bar();
bar();
xyz();
};
Foo.prototype.bar = function () {};
Run Code Online (Sandbox Code Playgroud)
var bar = module.exports = function () {};
Run Code Online (Sandbox Code Playgroud)
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinonChai');
var expect = chai.expect;
var Foo = require('../lib/foo');
chai.use('sinonChai');
describe('Foo', function () {
var method;
beforeEach(function (done) {
method = sinon.spy(Foo.prototype, 'bar'); …Run Code Online (Sandbox Code Playgroud) 我在NodeJS中编写API并使用Mocha,Chai和SuperTest进行测试.我正在使用一种典型的测试驱动方法来编写测试,然后用工作代码来满足这些测试.但是,由于所有不同排列的测试数量,我已经开始编写空占位符测试,以便我有所有it('should...')描述,以提醒我在获得该功能时要测试什么.例如:
it 'should not retrieve documents without an authorized user', (done) ->
done()
Run Code Online (Sandbox Code Playgroud)
这个问题是在done()没有任何断言的情况下调用,所以测试被认为是传递,所以我添加了以下断言.
false.should.equal true # force failure
Run Code Online (Sandbox Code Playgroud)
但它是一个黑客,Mocha显示失败的原因似乎令人困惑,特别是当其他完整测试可能失败时.
有没有官方方法在摩卡故意失败像这样的占位符测试?
我正在使用Mocha + Chai作为当前的Node.js项目.我的IDE是Webstorm 11.
不幸的是,似乎Webstorm无法弄清楚Chai的语言链应该/期望是如何工作的.以下是司空见惯的事:
我已经尝试通过显式设置两个Chai和Chai-DefinetlyTyped作为我的项目的库Settings->Javascript->Libraries,但这并没有解决任何问题.
为了工具支持,我考虑将assert的expect/should放弃,因为这样可以完美地工作.但是,我非常喜欢expect/should的风格,所以看到这个解决方案会很棒.
我对Javascript很新,我对Chai库进行单元测试有一个简单的问题.
当我在Chai库上研究一些材料时,我看到一个声明说"等于目标严格等于(===)到值"和eql"断言目标与值非常相等.".
但我对于严格和深刻的差异感到困惑.
chai ×10
mocha.js ×9
node.js ×6
javascript ×2
unit-testing ×2
arrays ×1
autocomplete ×1
codespaces ×1
istanbul ×1
sinon ×1
tdd ×1
typescript ×1
webstorm ×1