2016-10-06 3 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'});가 올바른 방법입니다. 나는 위에서 시험해 보았고, 재산이 아니라는 것에 대해 약간의 문제가 있었다. –