Javascript JS:发布单元测试中的“此”冲突?
我正在为我的meteor应用程序的出版物进行单元测试。我需要更改超时间隔,所以我添加了这个。timeout5000。但这给了我一个错误Javascript JS:发布单元测试中的“此”冲突?,javascript,unit-testing,meteor,this,chai,Javascript,Unit Testing,Meteor,This,Chai,我正在为我的meteor应用程序的出版物进行单元测试。我需要更改超时间隔,所以我添加了这个。timeout5000。但这给了我一个错误 Error: this.error is not a function at [object Object]._onTimeout 这来自于如果!我的发布文件的this.userId{返回this.error} 如何解决这个问题 如您所见,如果用户未登录,则发布将抛出一个错误。我想测试这个预期的错误 test.js server/publication.js
Error: this.error is not a function
at [object Object]._onTimeout
这来自于如果!我的发布文件的this.userId{返回this.error}
如何解决这个问题
如您所见,如果用户未登录,则发布将抛出一个错误。我想测试这个预期的错误
test.js
server/publication.js
确保您使用的是johanbrook:publication collector的最新版本
根据其源代码,1.0.10版本有错误方法:我认为您看到了超时,因为没有定义this.userId,所以publish永远不会返回 为了测试发布功能,我认为您需要: 1创建一个用户 2存根,即替换Meteor.user函数,该函数返回当前登录的用户登录方法 3将该用户的_id提供给PublicationsCollector,后者会将其发送到您的发布功能 我是这样做的:
import { Meteor } from 'meteor/meteor';
import { Factory } from 'meteor/dburles:factory';
import { PublicationCollector } from 'meteor/johanbrook:publication-collector';
import { resetDatabase } from 'meteor/xolvio:cleaner';
import faker from 'faker';
import { Random } from 'meteor/random';
import { chai, assert } from 'meteor/practicalmeteor:chai';
import sinon from 'sinon';
// and also import your publish and collection
Factory.define('user', Meteor.users, {
'name': 'Josephine',
});
if (Meteor.isServer) {
describe('Menus', () => {
beforeEach(function () {
resetDatabase();
const currentUser = Factory.create('user');
sinon.stub(Meteor, 'user');
Meteor.user.returns(currentUser); // now Meteor.user() will return the user we just created
// and create a Menu object in the Menus collection
});
afterEach(() => {
Meteor.user.restore();
resetDatabase();
});
describe('publish', () => {
it('can view menus', (done) => {
const collector = new PublicationCollector({ 'userId': Meteor.user()._id }); // give publish a value for this.userId
collector.collect(
'menus',
(collections) => {
assert.equal(collections.menus.length, 1);
done();
},
);
});
});
});
}
我忽略了要发布的对象的创建,因为您似乎已经在工作了。错误函数在哪里定义?它是meteor函数:
Meteor.publish('list', function (id) {
if (!this.userId) { return this.error() }
return Collection.find({})
})
import { Meteor } from 'meteor/meteor';
import { Factory } from 'meteor/dburles:factory';
import { PublicationCollector } from 'meteor/johanbrook:publication-collector';
import { resetDatabase } from 'meteor/xolvio:cleaner';
import faker from 'faker';
import { Random } from 'meteor/random';
import { chai, assert } from 'meteor/practicalmeteor:chai';
import sinon from 'sinon';
// and also import your publish and collection
Factory.define('user', Meteor.users, {
'name': 'Josephine',
});
if (Meteor.isServer) {
describe('Menus', () => {
beforeEach(function () {
resetDatabase();
const currentUser = Factory.create('user');
sinon.stub(Meteor, 'user');
Meteor.user.returns(currentUser); // now Meteor.user() will return the user we just created
// and create a Menu object in the Menus collection
});
afterEach(() => {
Meteor.user.restore();
resetDatabase();
});
describe('publish', () => {
it('can view menus', (done) => {
const collector = new PublicationCollector({ 'userId': Meteor.user()._id }); // give publish a value for this.userId
collector.collect(
'menus',
(collections) => {
assert.equal(collections.menus.length, 1);
done();
},
);
});
});
});
}