在Chai,您可以执行以下操作:
expect({}).to.exist;
Run Code Online (Sandbox Code Playgroud)
exist不是函数调用,但这仍然适用于测试框架.反向(expect({}).to.not.exist)导致测试失败,但同样,exist不是函数调用.
这些断言如何在不让我调用函数的情况下工作?事实上,如果我试图说expect({}).to.exist()测试失败,因为exist它不是一个函数.
我正在编写返回promises的代码的Javascript Mocha单元测试.我正在使用Chai作为承诺的图书馆.我希望以下最小单元测试失败.
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
chai.should();
var Promise = require("bluebird");
describe('2+2', function () {
var four = Promise.resolve(2 + 2);
it('should equal 5', function () {
four.should.eventually.equal(5);
})
});
Run Code Online (Sandbox Code Playgroud)
当我运行此测试时,我看到打印到控制台的断言错误,但测试仍然算作传递.
> mocha test/spec.js
2+2
? should equal 5
Unhandled rejection AssertionError: expected 4 to equal 5
1 passing (10ms)
Run Code Online (Sandbox Code Playgroud)
如何编写此测试以使失败的断言导致测试计为失败?
我正在尝试验证像这样的对象数组:
[
{
a: 1,
b: 2,
c: 3
},
{
a: 4,
b: 5,
c: 6
},
...
]
Run Code Online (Sandbox Code Playgroud)
包含至少一个带有{ a: 1 }和的对象{ c: 3 }:
我以为我可以用chai-things做到这一点,但我不知道该对象的所有属性都能够使用
expect(array).to.include.something.that.deep.equals({ ??, a: 1, c: 3});
Run Code Online (Sandbox Code Playgroud)
并且contain.a.thing.with.property不适用于多个属性:/
测试这样的东西的最佳方法是什么?
it('GET /customers/ with wrong id', (done) => {
request
.get(`/customers/${wrongId}`)
.end((err, res) => {
expect(res.body).to.equals({});
expect(res).to.have.status(404);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
1)客户CRUD GET /客户/ ID错误:
Uncaught AssertionError: expected {} to equal {}
+ expected - actual
Run Code Online (Sandbox Code Playgroud) 这是一个取消按钮
<div className="cancelFileBtn" onClick={this.props.cancelFileSending}>
Run Code Online (Sandbox Code Playgroud)
我需要模拟它的点击,我尝试了以下测试
wrapper.find('.cancelFileBtn').simulate('click');
Run Code Online (Sandbox Code Playgroud)
但点击功能仍未定义......我是否还错过了其他任何内容?如果有人在模拟中存在任何变化,那将会非常有帮助
<SendMessageButton onClick={this.props.handleClickSendMessage} loadingFile={this.props.loadingFile}/>
Run Code Online (Sandbox Code Playgroud) 我遇到了一个问题,在使用chai-http运行后我的mocha测试没有完成.测试后Mocha会挂起并最终进入超时状态(至少在我的CI上).
如果一个元素在页面上不可操作(在这种情况下,由另一个元素覆盖)并且您尝试单击它,赛普拉斯将显示如下错误:
CypressError: Timed out retrying: cy.click() failed because this element:
<span>...</span>
is being covered by another element:
Run Code Online (Sandbox Code Playgroud)
大!但有没有办法断言这是这种情况,又称无法点击该元素?
这不起作用:
should.not.exist - 元素确实存在should.be.disabled - 该元素未被禁用should.not.be.visible - 元素可见(刚被另一个透明元素覆盖)cy.on('uncaught:exception', ...),因为这不是一个例外我有两个混合订单数组
const actualResult = [
{
start: '30',
end: '50',
locations: ['loc1', 'loc2'],
},
{
start: '20',
end: '40',
locations: ['loc3', 'loc4'],
},
];
const expectedResult = [
{
start: '20',
end: '40',
locations: ['loc4', 'loc3'],
},
{
start: '30',
end: '50',
locations: ['loc2', 'loc1'],
},
];
Run Code Online (Sandbox Code Playgroud)
我编写了下面的代码来使用 chai 断言来比较它们
describe('test1', function () {
it('expect test', function () {
expect(actualResult).to.have.length(expectedResult.length);
for (b of expectedResult) {
var found = false;
for (d of actualResult) {
if (b.start == d.start && b.end …Run Code Online (Sandbox Code Playgroud) 我的里面有一个柴devDependencies:
"devDependencies": {
"@types/chai": "4.2.14",
"chai": "4.2.0",
}
Run Code Online (Sandbox Code Playgroud)
Chai 仅用于测试,但我不会发布测试文件。规则node/no-unpublished-import告诉我:
"chai" is not published
Run Code Online (Sandbox Code Playgroud)
从文档中:
这是为了防止 npm 发布后出现“找不到模块”错误。如果要导入 devDependency,请写入 .npmignore 或 package.json 的“files”字段。
我不明白“write .npmignore”建议是什么意思。我需要在那里写什么?目前,我有以下内容:
.idea/
/.eslintignore
/.eslintrc.yaml
/.gitignore
/.mocharc.yaml
/.npmignore
/tsconfig.json
**.test.ts
Run Code Online (Sandbox Code Playgroud)
最后一个忽略所有使用 chai 的测试文件。
背景:我目前正在阅读Ethan Brown撰写的" 使用Node和Express进行Web开发 "(顺便提一下这本学习节点和快递的好书),我坚持第5章 - 质量保险.
一切都很顺利.首先,我运行以下内容:
npm install --save-dev mocha
npm install -g mocha
npm install --save-dev chai
npm install --save-dev zombie
问题:然后我跑了(正如书中指示的那样):
mocha -u tdd -R spec qa/tests-crosspage.js 2>/dev/null
但这没有做任何事情.那么我跑了:
mocha -u tdd -R spec qa/tests-crosspage.js
然后出现这个错误:
/Users/esanz91/Desktop/CodingNotes/Node/MySite/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:405 var nonInheritedTags = new Set([
ReferenceError:未定义Set
版本: 为了给你们一个想法,我安装了以下版本.
CMD:
npm list --depth = 0
结果:
├──chai@2.2.0
├──express@ 4.12.3
├──express3-handlebars@0.5.2
├──mocha@2.2.4
└──zombie@4.0.7
此外,我正在运行节点0.10.
我该如何解决这个问题?
谢谢!