簡體   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