使用依赖注入和“完成"编写Karma + Mocha测试吗? [英] Writing Karma + Mocha tests with both dependency injection and `done`?

查看:82
本文介绍了使用依赖注入和“完成"编写Karma + Mocha测试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在摩卡中编写同时具有依赖项注入和done的Karma单元测试的最优雅方法是什么?

What's the most elegant way to write Karma unit tests in mocha that both have dependency injection and done?

依赖注入:

describe('cows', function(){
  it('farts a lot', inject(function(cow){
    // do stuff
  }))
})

完成:

describe('cows', function(){
  it('farts a lot', function(done){
    // do stuff
  })
})

如果我希望单元测试中同时提供cowdone怎么办?现在,这就是我正在做的,而且还不能令人满意.

What if I want both cow and done available in my unit test? Right now, this is what I'm doing, and it's unsatisfactory.

beforeEach(inject(function(cow){
  this.cow = cow;
}))

it('farts a lot', function(done){
  this.cow // etc
})

推荐答案

您可以将函数 inject 嵌套到测试函数中

You can nested function inject into test function

it("should nested inject function into test function", function(done) {
    inject(function($timeout) {

      $timeout(function() {
        expect(true).toBeTruthy();
        done();
      }, 10);

      $timeout.flush(10);

    });    
  });

inject 是在 ngMock 模块中定义的全局函数并可以在测试中的任何地方使用.

inject is global function defined in ngMock module and can be used anywhere in the test.

这篇关于使用依赖注入和“完成"编写Karma + Mocha测试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆