第261行发生了堆栈跟踪,如果在所有测试之前都调用了“之前”方法,则不会发生该错误。
我尝试使用async
和await
重构测试,并尝试调试测试用例以查看为什么未调用before方法。我已阅读并重新阅读了有关摩卡的文档。我对如何解决此问题感到iff愧。
test.js:
tables
.create({ <-- line 261.
golden_white: {
schema: {
id: "int auto_increment primary key",
first_name: "varchar(20) not null",
last_name: "varchar(20) not null"
}
}
})
包含引发堆栈跟踪的行的代码片段。
test.js-方法之前:
let docker = new Docker({ socketPath: "/var/run/docker.sock" });
let container = null;
let tables = null;
const databaseName = "goldpresi_tablesTest_database";
before(function(done) {
docker.container
.create({
Image: "mariadb:latest",
Env: ["MYSQL_ROOT_PASSWORD=passwd"]
})
.catch(err => done(err))
.then(c => {
container = c;
})
.then(c => c.status())
.then(status => {
pool = mysql2.createPool({
username: "root",
password: "password",
host: `${status.data.NetworkSettings.IPAddress}`
});
pool
.query(`create database ${databaseName}`)
.then(() => pool.query(`use database ${databaseName}`))
.then(() => {
tables = new Tables(pool);
done()})
.catch(err => done(err));
});
});
堆栈跟踪:
/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/node_modules/yargs/yargs.js:1163
else throw err
^
TypeError: Cannot read property 'create' of null
at Suite.<anonymous> (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/backend/test/build/test.js:264:10)
at Object.create (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/interfaces/common.js:140:19)
at context.describe.context.context (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/interfaces/bdd.js:42:27)
at Suite.<anonymous> (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/backend/test/build/test.js:261:5)
at Object.create (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/interfaces/common.js:140:19)
at context.describe.context.context (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/interfaces/bdd.js:42:27)
at Suite.<anonymous> (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/backend/test/build/test.js:260:3)
at Object.create (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/interfaces/common.js:140:19)
at context.describe.context.context (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/interfaces/bdd.js:42:27)
at Object.<anonymous> (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/backend/test/build/test.js:10:1)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at /home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/mocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/mocha.js:327:14)
at Mocha.run (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/mocha.js:804:10)
at Object.exports.singleRun (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/cli/run-helpers.js:207:16)
at exports.runMocha (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/cli/run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/cli/run.js:296:3)
at Object.runCommand (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/node_modules/yargs/lib/command.js:242:26)
at Object.parseArgs [as _parseArgs] (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/node_modules/yargs/yargs.js:1104:24)
at Object.parse (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/node_modules/yargs/yargs.js:566:25)
at Object.exports.main (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/lib/cli/cli.js:63:6)
at Object.<anonymous> (/home/duke/Documents/Development/Web Development/higher_and_further_courses_web_app/node_modules/mocha/bin/_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:266:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)
Test.js:https://github.com/TheMasteredPanda/Goldpresi-Website-Application/blob/master/backend/test/build/test.js Tables.js:https://github.com/TheMasteredPanda/Goldpresi-Website-Application/blob/master/backend/src/scripts/dataManager/tables.js
我希望调用位于父“描述”中的before方法,然后对所有其他测试用例进行程序调用,并在控制台中打印期望的文本。