我正在为我的应用程序使用node,mocha和chai.我想测试我的返回结果数据属性与我的模型对象之一是"对象类型".(与柴的实例非常相似).我只是想确认这两个对象具有相同的属性名称集.我特别对属性的实际值不感兴趣.
让我们说我有像下面的模型人.我想检查我的results.data是否具有与预期模型相同的属性.所以在这种情况下,Person具有firstName和lastName.
因此,如果results.data.lastName与results.data.firstName同时存在,那么它应该返回true.如果其中任何一个不存在,则应返回false.如果results.data具有任何其他属性(如results.data.surname),那么它将返回false,因为在Person中不存在姓氏.
该模型
function Person(data) {
var self = this;
self.firstName = "unknown";
self.lastName = "unknown";
if (typeof data != "undefined") {
self.firstName = data.firstName;
self.lastName = data.lastName;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为我的NodeJS代码编写JS UT.我使用Chai作为断言库,我更喜欢should语法.我还使用jslint检查JS文件语法,甚至是UT的JS文件.
现在我遇到了jslint和Chai的问题.在柴,您可以使用:
myvalue.should.be.true;
Run Code Online (Sandbox Code Playgroud)
但是jslint给了我:
#1 Expected an assignment or function call and instead saw an expression.
Run Code Online (Sandbox Code Playgroud)
我知道jslint有很多选项,但我找不到合适的选项来关闭这个检查.
我正在尝试对我的reactjs组件进行单元测试:
import React from 'react';
import Modal from 'react-modal';
import store from '../../../store'
import lodash from 'lodash'
export class AddToOrder extends React.Component {
constructor(props) {
super(props);
this.state = {checked: false}
//debugger
}
checkBoxChecked() {
return true
}
render() {
console.log('testing=this.props.id',this.props.id )
return (
<div className="order">
<label>
<input
id={this.props.parent}
checked={this.checkBoxChecked()}
onChange={this.addToOrder.bind(this, this.props)}
type="checkbox"/>
Add to order
</label>
</div>
)
}
}
export default AddToOrder;Run Code Online (Sandbox Code Playgroud)
刚开始我已经在努力断言checkBoxChecked方法:
import React from 'react-native';
import {shallow} from 'enzyme';
import {AddToOrder} from '../app/components/buttons/addtoorder/addtoorder';
import {expect} from 'chai'; …Run Code Online (Sandbox Code Playgroud)在我的测试中柴,我经常发现自己想要使用他们的主张是一样的东西.to.be.empty,.to.be.true等等,因为我觉得他们更清洁,而不是读.to.be.length(1)或.to.be.equal(true).然而,这打破了我的linter(我正在使用默认的Airbnb linting).
我可以使用// disable-eslint-line语法,但是我必须将它添加到每个读取的内容中,这看起来很乏味.
我也读过关于DirtyChai库的内容,但这需要我回到我的整个测试库中,为它们添加括号,这些似乎是我不应该做的只是为了让我的linter传递它应该可能的东西一开始就好.
有没有人知道比我上面概述的更好的方法来处理这个问题?
我想测试一个返回promise的函数.
在这个特定的测试中,预期承诺将被拒绝,其中包含经典message字段的Error对象(在此测试中,它应该相等"my error message")和我添加的自定义字段named code,这是一个字符串(如"EACCESS"," ERIGHT"等,在这个测试中它预计会相等"EFOO")
我想使用chai-as-promised.
return expect(foo()).to.eventually.be.rejectedWith("my error message");
Run Code Online (Sandbox Code Playgroud)
这个断言正在起作用,但现在我也想测试这个code领域.
怎么做?
如何在每次测试前重置Sinon间谍的"被叫"计数?
这就是我现在正在做的事情:
beforeEach(function() {
this.spied = sinon.spy(Obj.prototype, 'spiedMethod');
});
afterEach(function() {
Obj.prototype.spiedMethod.restore();
this.spied.reset();
});
Run Code Online (Sandbox Code Playgroud)
但是当我在测试中检查呼叫计数时:
it('calls the method once', function() {
$.publish('event:trigger');
expect(this.spied).to.have.been.calledOnce;
});
Run Code Online (Sandbox Code Playgroud)
...测试失败并报告该方法被调用X次(每次上一次测试也触发同一事件一次).
我在基于角度的网站上有一个设置,可以打开和关闭下拉列表.如果它关闭,则它不会显示在主页面上.
使用量角器时,我需要检查开关关闭时是否存在此元素.但是,我不应该被抛入Element Not Found Error,因为它是一组中的一个测试.我该怎么做?
我试过这样做:
expect($$('.switch').count()).to.equal(0).and.notify(next);
Run Code Online (Sandbox Code Playgroud)
但我得到了一个AssertionError ...
我如何or用chai.should 进行测试?
例如
total.should.equal(4).or.equal(5)
Run Code Online (Sandbox Code Playgroud)
要么
total.should.equal.any(4,5)
Run Code Online (Sandbox Code Playgroud)
什么是正确的语法?我在文档中找不到任何内容
试图测试一些抛出Mocha/Chai异常的代码,但没有运气,这是我试图测试的简单代码:
class window.VisualizationsManager
test: ->
throw(new Error 'Oh no')
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
describe 'VisualizationsManager', ->
it 'does not permit the construction of new instances', ->
manager = new window.VisualizationsManager
chai.expect(manager.test()).to.throw('Oh no')
Run Code Online (Sandbox Code Playgroud)
但是,当我运行规范时,测试失败并抛出异常.
Failure/Error: Oh no
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
让我们说我想写这个简单的任务.但是我想写一个测试验证:
我正在测试摩卡和柴期待.
提前致谢.我已经尝试了所有可能出现的变体,但无法提出解决方案.
var util = require('util'),
EventEmitter = require('events').EventEmitter;
function SomeTask() {
var self = this;
setInterval(function() {
self.emit('data', { name: 'name' });
}, 5000);
}
util.inherits(SomeTask, EventEmitter);
module.exports = SomeTask;
Run Code Online (Sandbox Code Playgroud) chai ×10
javascript ×6
mocha.js ×4
node.js ×3
unit-testing ×2
angularjs ×1
coffeescript ×1
enzyme ×1
eslint ×1
jslint ×1
protractor ×1
reactjs ×1
sinon ×1