繁体   English   中英

流星和摩卡柴:测试插入功能

[英]Meteor & Mocha Chai : test insert function

我正在尝试测试我的插入功能,但UserAccount失败:错误:无法读取未定义的属性“用户名”

我不知道如何通过测试才能插入帖子,这是我的方法:

Meteor.methods({

  'posts.insert'(title, content) {
     check(title, String);
     check(content, String);


     if (! this.userId) {
         throw new Meteor.Error('not-authorized');
     }

     const urlSlug = getSlug(title);

     Posts.insert({
        title,
        content,
        urlSlug,
        createdAt: new Date(),
        owner: this.userId,
        username: Meteor.users.findOne(this.userId).username,
     });
  },

});

这是我要测试的测试方法:

if (Meteor.isServer) {
    describe('Posts', () => {
        describe('methods', () => {
            const userId = Random.id();
            let postId;

            beforeEach(() => {
                Posts.remove({});

                postId = Posts.insert({
                    title: 'test post',
                    content: 'test content',
                    urlSlug: 'test-post',
                    createdAt: new Date(),
                    owner: userId,
                    username: 'toto',
                });
            });

            // TEST INSERT METHOD
            it('can insert post', () => {
                const title = "test blog 2";
                const content = "test content blog 2";

                const insertPost Meteor.server.method_handlers['posts.insert'];
                const invocation = { userId };
                insertPost.apply(invocation, [title, content]);
                assert.equal(Posts.find().count(), 2);
            });
       });
   });
 }

请问你能帮帮我吗 ?

如何使用sinon来对流星电话进行打桩 尽管我不确定它是否适用于嵌套对象(如果有人可以确认)。

sinon.stub(Meteor, 'users.findOne').returns({ username: 'Foo' });

并且不要忘记在使用它后进行还原afterEach()例如,在afterEach()中)。

Meteor.users.findOne.restore();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM