AVA测试问题
AVA testing problems
我正在尝试使用 AVA 编写测试,但我似乎无法让它正常工作。 fn
通过我的所有函数传递回调函数,并在完成所有操作后调用它。我的测试是
import test from 'ava';
import fn from './index.js';
test('NonLiteral && Literal', function (t) {
fn('test.txt', '', function (res) {
console.log(res);
t.is(res, '');
});
});
资源是
This is a test
How is it going
So far!!!
但是它说我的测试通过了。我一直在关注 this 测试。这是我一直在看的片段
test('throwing a named function will report the to the console', function (t) {
execCli('fixture/throw-named-function.js', function (err, stdout, stderr) {
t.ok(err);
t.match(stderr, /\[Function: fooFn]/);
// TODO(jamestalmage)
// t.ok(/1 uncaught exception[^s]/.test(stdout));
t.end();
});
});
有人可以向我解释我做错了什么吗?
很抱歉造成混淆,不幸的是,您正在查看的单元测试使用 tap
,而不是 AVA。 (AVA 还没有将自己用于测试……)。
我猜 fn
是异步的。在这种情况下,您可能想要使用 test.cb
.
test.cb('NonLiteral && Literal', function (t) {
fn('test.txt', '', function (res) {
console.log(res);
t.is(res, '');
t.end();
});
});
现在,看起来 fn
可能会多次调用该回调,但多次调用 t.end()
是错误的。如果是这样,您将需要执行以下操作:
test.cb('NonLiteral && Literal', function (t) {
var expected = ['foo', 'bar', 'baz'];
var i = 0;
fn('test.txt', '', function (res) {
t.is(res, expected[i]);
i++;
if (i >= expected.length) {
t.end();
}
});
});
最后,我鼓励您考虑实施基于 api 的 Promise,这样您就可以利用 async
函数和 await
关键字。它最终创建了比回调更简洁的代码。在您想多次调用回调的情况下,请考虑 Observables。 AVA 文档中记录了两者的测试策略。通过谷歌搜索很容易找到关于 Observables 的更多信息。
感谢您试用 AVA。继续问这些问题!
我正在尝试使用 AVA 编写测试,但我似乎无法让它正常工作。 fn
通过我的所有函数传递回调函数,并在完成所有操作后调用它。我的测试是
import test from 'ava';
import fn from './index.js';
test('NonLiteral && Literal', function (t) {
fn('test.txt', '', function (res) {
console.log(res);
t.is(res, '');
});
});
资源是
This is a test
How is it going
So far!!!
但是它说我的测试通过了。我一直在关注 this 测试。这是我一直在看的片段
test('throwing a named function will report the to the console', function (t) {
execCli('fixture/throw-named-function.js', function (err, stdout, stderr) {
t.ok(err);
t.match(stderr, /\[Function: fooFn]/);
// TODO(jamestalmage)
// t.ok(/1 uncaught exception[^s]/.test(stdout));
t.end();
});
});
有人可以向我解释我做错了什么吗?
很抱歉造成混淆,不幸的是,您正在查看的单元测试使用 tap
,而不是 AVA。 (AVA 还没有将自己用于测试……)。
我猜 fn
是异步的。在这种情况下,您可能想要使用 test.cb
.
test.cb('NonLiteral && Literal', function (t) {
fn('test.txt', '', function (res) {
console.log(res);
t.is(res, '');
t.end();
});
});
现在,看起来 fn
可能会多次调用该回调,但多次调用 t.end()
是错误的。如果是这样,您将需要执行以下操作:
test.cb('NonLiteral && Literal', function (t) {
var expected = ['foo', 'bar', 'baz'];
var i = 0;
fn('test.txt', '', function (res) {
t.is(res, expected[i]);
i++;
if (i >= expected.length) {
t.end();
}
});
});
最后,我鼓励您考虑实施基于 api 的 Promise,这样您就可以利用 async
函数和 await
关键字。它最终创建了比回调更简洁的代码。在您想多次调用回调的情况下,请考虑 Observables。 AVA 文档中记录了两者的测试策略。通过谷歌搜索很容易找到关于 Observables 的更多信息。
感谢您试用 AVA。继续问这些问题!