使用摩卡的异步'做'功能角旁边的喷油器 [英] Using mocha's async 'done' function alongside angular's injector

查看:132
本文介绍了使用摩卡的异步'做'功能角旁边的喷油器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己无法弄清楚如何使用摩卡的异步完成的功能与angulars注射器。我已经试过这样的东西:

I find myself unable to figure out how to use mocha's async done function with angulars injector. I've tried stuff like this:

describe('myService', function() {

    beforeEach(module('myModule'));

    describe('action()', function() {
        var obj = {};
        before(function(done) {
            obj.done = done;
        });

        it('calls a function async', inject(function(myService) {
            myService.calledAsync = function() {
                obj.done()
             };

             myService.action();
        });

    });
});

但是,这并不工作,因为函数永远不会运行。因此,之前只是超时。

But that doesn't work because the it function is never run. So the before just times out.

我也试过缠绕的第二个参数注入来描述和函数之前运行里面的整个测试体(此处TJ的建议:的 https://github.com/mochajs/mocha/issues/253 )。但是,这给我的错误遗漏的类型错误:无法读取属性空的$模块

I've also tried wrapping inject around the second argument to describe and running the entire test body inside the before function (as suggested by tj here: https://github.com/mochajs/mocha/issues/253). But that gives me the error Uncaught TypeError: Cannot read property '$modules' of null.

有没有人有做这项工作的方法?

Does anyone have a method for making this work?

推荐答案

我认为你需要解决的主要问题是:1,不要试图挽救完成之前,后来把它和2.移动关闭 beforeEach 所以你的可刚函数(完成){ 正常。

I think the main issues you need to fix are 1. Don't try to save done during before and call it later and 2. Move the inject off the it up to the beforeEach so your it can be just function(done) { as normal.

下面是为我工作的布局样板:

Here's the layout boilerplate that is working for me:

describe('myService', function () {
  // declare injected variables here so they are available to tests
  // via closure
  var httpBackend, rootScope, controller, myService;

  // Then mock out your module with a beforeEach
  beforeEach(module('myModule'));

  // inject another beforeEach to get what you need
  beforeEach(inject(function ($rootScope, $controller, $httpBackend, _myService_) {
    httpBackend = $httpBackend;
    rootScope = $rootScope;
    controller = $controller;
    myService = _myService_;
    });
  });

  // Just an example
  afterEach(function() {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
  });

  // Do a regular `it` function
  it('should ', function(done) {
    myService.calledAsync = function() {
      done();
    };
    myService.action();
  });
});

从片段的一些杂项技巧:

A few misc tips from your snippet:


  • 请不要尝试存储一个参考完成。每一个生命周期的观点是不同的,因此,如果摩卡为您提供了 beforeEach A 完成函数具有语义beforeEach完成当这种被称为,所以如果你想要保存它,后来把它里面的摩卡的控制流会搞的一团糟。

  • 摩卡大多是实用的风格,而不是面向对象方法的风格,所以分配完成来一个对象,并调用它像一个方法是没有必要的或预期。你可以叫()完成直接

  • Don't try to store a reference to done. Each lifecycle point is distinct, so if mocha gives you a done function in beforeEach that has the semantic "beforeEach is done when this gets called", so if you try to save it and call it later inside it mocha's control flow will get messed up.
  • Mocha is mostly functional style and not OO method style, so assigning done to an object and calling it like a method is not necessary or intended. You can just call done() directly.

这篇关于使用摩卡的异步'做'功能角旁边的喷油器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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