JS:'this' 发布单元测试中的冲突?

JS: 'this' collision in unit test for publication?

我正在对我的 meteor 应用程序的发布进行单元测试。我需要更改超时间隔,所以我添加了 this.timeout(5000)。但这给了我错误

Error: this.error is not a function
at [object Object]._onTimeout

来自我的发布文件 if (!this.userId) { return this.error() }

如何解决这个问题?

如您所见,如果用户未登录,发布应该会抛出错误。我想测试这个预期错误。

test.js

import { expect } from 'meteor/practicalmeteor:chai'
import { PublicationCollector } from 'meteor/johanbrook:publication-collector'

describe('Publication', () => {
  it('should not return data', function (done) {
    this.timeout(5000)
    const collector = new PublicationCollector()
    collector.collect('list', (collections) => {
      expect(collections.collection).to.be.undefined
      done()
    })
  })
})

server/publication.js

Meteor.publish('list', function (id) {
  if (!this.userId) { return this.error() }
  return Collection.find({})
})

确保您使用的是最新版本的 johanbrook:publication-collector

根据其源代码,1.0.10 版本 具有 error() 方法:https://github.com/johanbrook/meteor-publication-collector/blob/v1.0.10/publication-collector.js#L187

我认为您看到超时是因为 this.userId 未定义,所以永远不要发布 returns。

为了测试您的发布功能,我认为您需要:

1) 创建用户

2) 存根即替换 Meteor.user() 方法中的 returns 当前登录用户的函数

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();
                    },
                );
            });
        });
    });
}

我省略了要发布的对象的创建,因为您似乎已经在工作了。