How to unit test a meteor method with practicalmeteor:mocha

后端 未结 1 1191
半阙折子戏
半阙折子戏 2021-01-18 03:17

I\'m having a nightmare writing unit tests for meteor. There are too many old, outdated articles and too few clear, relevant pieces of documentation for me to be able to wor

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 03:35

    I think you need to stud the Meteor.user() method, try this:

    import { Meteor } from 'meteor/meteor';
    import { resetDatabase } from 'meteor/xolvio:cleaner';
    import { sinon } from 'meteor/practicalmeteor:sinon';
    
    describe('...', () => {
      let currentUser;
    
      beforeEach(() => {
        resetDatabase();
        Factory.define('user', Meteor.users, {
          profile: {
            // ...
          },
        });
        currentUser = Factory.create('user');
        sinon.stub(Meteor, 'user');
        Meteor.user.returns(currentUser);
      });
    
      afterEach(() => {
        Meteor.user.restore();
        resetDatabase();
      });
    
      it('...', () => {
        // ...
      });
    });
    

    You need to add these packages: dburles:factory, xolvio:cleaner, practicalmeteor:sinon

    0 讨论(0)
提交回复
热议问题