【问题标题】:JS: 'this' collision in unit test for publication?JS:发布单元测试中的“this”冲突?
【发布时间】:2017-08-10 09:57:13
【问题描述】:

我正在对我的流星应用程序的出版物进行单元测试。我需要更改超时间隔,所以我添加了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({})
})

【问题讨论】:

标签: javascript unit-testing meteor this chai


【解决方案1】:

我认为您会看到超时,因为 this.userId 未定义,因此发布永远不会返回。

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

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

我已经省略了要发布的对象的创建,因为您似乎已经可以使用它了。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 2016-12-09
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多