使用摩卡的异步'做'功能角旁边的喷油器 [英] Using mocha's async 'done' function alongside angular's injector
问题描述
我发现自己无法弄清楚如何使用摩卡的异步完成的功能与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 adone
function inbeforeEach
that has the semantic "beforeEach is done when this gets called", so if you try to save it and call it later insideit
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 calldone()
directly.
这篇关于使用摩卡的异步'做'功能角旁边的喷油器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!