繁体   English   中英

MeteorJS:如何在单元测试中存根验证方法

[英]MeteorJS: How to stub validated method in unit test

我正在使用经过验证的方法( mdg:validated-method )和LoggedInMixin( tunifight:loggedin-mixin )。

现在我的单元测试有问题,因为它们因notLogged错误而失败,因为在单元测试中当然没有记录用户。 我该怎么做那个?

方法

const resetEdit = new ValidatedMethod({
  name: 'reset',
  mixins: [LoggedInMixin],
  checkLoggedInError: { error: 'notLogged' }, // <- throws error if user is not logged in
  validate: null,

  run ({ id }) {
    // ...
  }
})

单元测试

describe('resetEdit', () => {
  it('should reset data', (done) => {
    resetEdit.call({ id: 'IDString' })
  })
})

单元测试抛出Error: [notLogged]

编辑:

validated-method有一种内置的提供上下文的方法,它在README ,完全适用于你问题中的情况。

方法#_execute(context:Object,args:Object)

从您的测试代码中调用此方法来模拟代表特定用户调用方法:

来源

  it('should reset data', (done) => {
      resetEdit._execute({userId: '123'}, { id: 'IDString' });
      done();
  });

原始答案:

我相信这可以使用DDP._CurrentMethodInvocation流星环境变量来实现。

如果在其值为userId字符串的作用域中运行测试,则它将与方法调用上下文对象的其余部分合并,并且mixin不会失败。

describe('resetEdit', () => {
  it('should reset data', (done) => {
    DDP._CurrentMethodInvocation.withValue({userId: '123'}, function() {
      console.log(DDP._CurrentInvocation.get()); // {userId: '123'}
      resetEdit.call({ id: 'IDString' });
      done();
    })
  });
})

暂无
暂无

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

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