2016-10-06 11 views
2

私は私の挿入機能をテストしようとしているが、それは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); 
      }); 
     }); 
    }); 
} 

はあなたが私にしてください助けてもらえますか?

答えて

0

sinonを使用して流星声をスタブするのはどうですか?ネストされたオブジェクトで動作するかどうかはわかりませんが(誰かが確認できる場合)

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

そして、あなたは(たとえばafterEach()で)それを使用した後にそれを復元することを忘れないでください。

Meteor.users.findOne.restore(); 
+0

'sinon.stub(Meteor.users、 'findOne')。返信({username: 'Foo'});は正しい方法です。私は上記のことを試みて、プロパティではないことについていくつかの問題があります。 –

関連する問題