Meteor服务器端TypeScript的单元测试

时间:2018-04-11 12:17:35

标签: unit-testing typescript meteor import server-side

我的服务器端单元测试有问题。

我的测试是下一个:

import { Meteor } from 'meteor/meteor';
import { Article } from '../../../imports/models/article';
import { Articles } from '../../../imports/collections/articles';
import './articles';
import { Random } from 'meteor/random';
import {Rate} from "../../../imports/models/rate.model";
import { expect, assert } from 'chai';
import {Observable} from "rxjs/Observable";

if (Meteor.isServer) {
    describe('Articles', () => {

        const userId = Random.id();

        beforeEach(() => {
            StubCollections.add([Articles]);
            StubCollections.stub();
            Articles.remove({});
        });

        it('can delete owned article', async (done) => {

            const articleId = await Articles.insert({
                title: "string",
                content: "string",
                owner: userId,
                picture_url: "string",
                source: "string",
                createdAt: new Date()
            }).toPromise();


            const deleteArticle = Meteor.server.method_handlers["removeArticle"];
            // // Run the method with `this` set to the fake invocation
            //`enter code here`
            const invocation = {userId};

            deleteArticle.apply(invocation, [articleId]);

            console.log(articleId);

            const count = await Articles.find({}).count().toPromise();

            // Verify that the method does what we expected
            expect(count).equal(0);
            StubCollections.restore();
            done()
        });
    });
}

我无法导入存根集合,因为打字稿找不到它。

我已经尝试在tsconfig.json上安装meteor服务器包但是我没有成功。

当我删除StubCollection时,当" Articles.find({})"

时,我有一个2秒的超时时间

您有解决问题的想法吗?

1 个答案:

答案 0 :(得分:0)

我的问题在于使用Meteor.observable的Observable类型的Article.find({})结果。

我的新测试是下一个

    it('can delete owned article', async done => {

        let fixFindToPromise: number  = 0;

        const articleId = await Articles.insert({
            title: "string",
            content: "string",
            owner: userId,
            picture_url: "string",
            source: "string",
            createdAt: new Date()
        }).toPromise();
        // console.log('A2', articleId2);

        const deleteArticle = Meteor.server.method_handlers["removeArticle"];
        // Run the method with `this` set to the fake invocation
        deleteArticle.apply({userId}, [articleId]);


        // Find the internal implementation of the task method so we can
        console.log("ArticleId:", articleId);

        Articles.find().subscribe((countLog) => {
            fixFindToPromise++;

            if (fixFindToPromise == 1 ) {
                if ( countLog.length == 0 ) {
                    done();
                } else {
                    done("Count not correct");
                }
            }
        });

可能使用具有承诺兼容性结果的find方法以获得更好的语法并使用assert和expect吗? 并且在打字稿单元测试服务器端存在导入包的项目或解决方案?

感谢您的回复