我在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显示失败的原因似乎令人困惑,特别是当其他完整测试可能失败时.
有没有官方方法在摩卡故意失败像这样的占位符测试?
我正在努力了解Sinon,并希望窥探console.log.代码很简单:
function logToConsole() {
console.log('Hello World');
}
exports.logToConsole = logToConsole;
Run Code Online (Sandbox Code Playgroud)
但是,如果我想测试它,它不起作用,因为调用console.log没有在被测系统内注册:
var chai = require('chai'),
expect = chai.expect,
sinonChai = require('sinon-chai'),
sinon = require('sinon'),
sut = require('../src/logToConsole');
chai.use(sinonChai);
describe('logToConsole', function() {
it('should spy on console.log', function() {
sinon.spy(console, 'log');
sut.logToConsole();
expect(console.log).to.have.been.called;
});
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我console.log在测试本身内部执行,则会捕获并传递:
it('should spy on console.log', function() {
sinon.spy(console, 'log');
sut.logToConsole();
console.log('Test');
expect(console.log).to.have.been.called;
});
Run Code Online (Sandbox Code Playgroud)
有趣的是,它似乎根本无法监视内部函数调用.这不是间谍图书馆的目的吗?
例如
function a() {};
function b() {
a();
}
Run Code Online (Sandbox Code Playgroud) 如果一个Array包含某个元素,Chai有一个很好的断言方法
expect([1,2,3]).to.include(2);
Run Code Online (Sandbox Code Playgroud)
给定一个对象数组,我想要的是类似的东西:
expect([{a:1},{b:2}]).to.include({b:2});
Run Code Online (Sandbox Code Playgroud)
这可能吗?
执行以下单元测试给出"错误:[$ injector:unpr]未知提供者:$ stateProvider < - $ state".我已将angular-ui-router.min.js附加在karma文件中.
describe("Unit tests", function() {
var $rootScope, $injector, $state;
console.log("hello");
beforeEach(inject(function(_$rootScope_, _$state_, _$injector_, $templateCache) {
console.log("hello1");
$rootScope = _$rootScope_;
$injector = _$injector_;
$state = _$state_;
}));
describe("states", function() {
it("verify state configuration", function() {
var config = $state.get("DRaaS");
console.log(config, "cc");
});
});
});
Run Code Online (Sandbox Code Playgroud) 在使用moment函数调用它时,我无法存根构造format函数以返回预定义的字符串,这是我想要运行的示例规范mocha:
it('should stub moment', sinon.test(function() {
console.log('Real call:', moment());
const formatForTheStub = 'DD-MM-YYYY [at] HH:mm';
const momentStub = sinon.stub(moment(),'format')
.withArgs(formatForTheStub)
.returns('FOOBARBAZ');
const dateValueAsString = '2025-06-01T00:00:00Z';
const output = moment(dateValueAsString).format(formatForTheStub);
console.log('Stub output:',output);
expect(output).to.equal('FOOBARBAZ');
}));
Run Code Online (Sandbox Code Playgroud)
我能够看到这个输出使用console.log:
Real call: "1970-01-01T00:00:00.000Z"
Stub output: 01-06-2025 at 01:00
Run Code Online (Sandbox Code Playgroud)
但是测试失败导致01-06-2025 at 01:00 !== 'FOOBARBAZ'
我怎样才能正确存根moment(something).format(...)?
我现在用的是写一些测试摩卡测试框架和柴断言中图书馆我在Chrome浏览器一直在测试这些,他们工作得很好,但后来我尝试他们在无头的浏览器摩卡phantomjs和.点击()事件,我发送某些元素似乎不会被解雇,而其他人正在工作.
测试成功登录到网站,使用以下方式完成:
$("#login").click()
Run Code Online (Sandbox Code Playgroud)
"#login"是表单提交按钮,单击此方式可以正常工作.
接下来我想点击一个作为锚元素的菜单项.
$("#menuItemToClick").click();
Run Code Online (Sandbox Code Playgroud)
这是它停止在mocha-phantomjs工作的地方,即使这在chrome中运行良好.
所以很明显.click()函数已经定义并且可以工作但不适用于这个元素.
我已经筋疲力尽了google-foo.以下是我见过的其他一些提及和试过的事情:
var evObj = new CustomEvent('click');
evObj.initEvent('click', true, false);
document.getElementById('menuItemToClick').dispatchEvent(evObj);
Run Code Online (Sandbox Code Playgroud)
这会触发click事件并将浏览器带到正确的url,但它也会导致整个页面重新加载,在chrome和mocha-phantomjs中,从一开始就重新启动测试,将它们置于无限循环中......是否存在一种使用它的方法,而不会导致页面重新加载?
var elementPosition = $("#menuItemToClick").offset();
page.sendEvent('click', elementPosition.left + 1, elementPosition.top + 1);
Run Code Online (Sandbox Code Playgroud)
如果您传递元素的正确坐标,则应该发送本机单击事件.我试图让它工作但我似乎无法从正在运行的mocha测试中访问该页面.有没有办法从mocha-phantomjs测试中访问phantomjs页面变量?
任何帮助是极大的赞赏!
我是js单元测试的新手,我正在尝试使用mocha作为我在这个github repo上找到的骨干联系管理器教程.但是,我有一个全局window.ContactManager变量,我想要测试它是否存在,然后测试启动函数内的router.on功能.变量看起来像这样:
window.ContactManager = {
Models: {},
Collections: {},
Views: {},
start: function(data) {
var contacts = new ContactManager.Collections.Contacts(data.contacts),
router = new ContactManager.Router();
router.on('route:home', function() {
router.navigate('contacts', {
trigger: true,
replace: true
});
});
router.on('route:showContacts', function() {
var contactsView = new ContactManager.Views.Contacts({
collection: contacts
});
.....
Run Code Online (Sandbox Code Playgroud)
我的测试不起作用:var expect = require('chai').expect;
describe("Application", function() {
it('creates a global variable for the name space ContactManager' , function () {
expect(ContactManager).to.exist;
})
});
Run Code Online (Sandbox Code Playgroud)
如何通过在控制台中运行测试来测试和访问mocha中的全局窗口变量?
从chai的api你有这样的代码:
.exist
Asserts that the target is neither null nor undefined.
var foo = 'hi'
, bar = null
, baz;
expect(foo).to.exist;
expect(bar).to.not.exist;
expect(baz).to.not.exist;
Run Code Online (Sandbox Code Playgroud)
如何存在部分工作?expect函数返回一个对象,然后在"to"对象上只有一个属性查找.这只是一个房产评估,不是吗?对我来说唯一有意义的是,如果存在属性是一个getter方法.
怎么回事?
我正在使用node.js构建一个应用程序并使用mocha + chai进行测试.有没有办法可以为我的GET和POST chai请求添加自定义标头?
例如,我想要(半伪代码):
chai.request(server)
.get('/api/car/' + data.car_id)
.headers({'some_custom_attribute':'some_value'})
.end(function(err, res) {
//do something
});
Run Code Online (Sandbox Code Playgroud)
同样的帖子:
chai.request(server)
.post('/api/car/')
.headers({'some_custom_attribute':'some_value'})
.send({car_id: 'some_car_id'})
.end(function(err, res) {
//do something
});
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
提前致谢!
我的渲染功能中有一个FileInput
<FileInput
accept= "image/jpeg,image/png,audio/mp3"
onChange= {this.fileInputOnChange}
children= {<i className="fa fa-paperclip"/>}
className= 'fileInput'
/>
Run Code Online (Sandbox Code Playgroud)
我需要编写一个文件上传测试,当我模拟更改函数时,它调用函数fileInputOnChange
fileInputOnChange: function(evt){
var file = evt.target.files[0];
var fileReader = new FileReader();
fileReader.onload = function(readFile){
// Check the file type.
var fileType = file.type.toLowerCase();
if(fileType.lastIndexOf('image') === 0 && (fileType.lastIndexOf('png') >= 0 || fileType.lastIndexOf('jpeg'))){
var image = new Image();
image.onload = function(){
var attachedFile = {
attached: file,
mediaSource: readFile.target.result,
type: 'image'
}
this.props.onChange(attachedFile);
}.bind(this);
image.onerror = function(){
this.props.onError("INVALID_TYPE");
}.bind(this);
image.src = readFile.target.result;
}else if(fileType.lastIndexOf('audio') === 0 && …Run Code Online (Sandbox Code Playgroud)