我正在使用 Restify,我正在尝试学习如何编写测试我的 CRUD 方法。当我连接到 DynamoDB 时,我可以在 Postman 上毫无问题地测试它们。对于测试,我将连接到在 localhost:8000 上运行的本地 DynamoDB。
上server.listen
,我有这个:
dynamoose.AWS.config.update({
accessKeyId: config.db.aws_access_key_id,
secretAccessKey: config.db.aws_secret_access_key,
region: config.db.aws_region
});
if (config.env == "test") {
dynamoose.local();
}
(我也尝试调用dynamoose.local()
上述更新)
当我使用mocha --timeout 10000
命令运行测试时,出现以下错误:
1) Uncaught error outside test suite:
Uncaught ConfigError: Missing region in config
at Request.VALIDATE_REGION (node_modules/aws-sdk/lib/event_listeners.js:91:45)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at callNextListener (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
at /Users/username/Development/projectname-api/node_modules/aws-sdk/lib/event_listeners.js:85:9
at finish (node_modules/aws-sdk/lib/config.js:320:7)
at /Users/username/Development/projectname-api/node_modules/aws-sdk/lib/config.js:338:9
at SharedIniFileCredentials.get (node_modules/aws-sdk/lib/credentials.js:126:7)
at getAsyncCredentials (node_modules/aws-sdk/lib/config.js:332:24)
at Config.getCredentials (node_modules/aws-sdk/lib/config.js:352:9)
at Request.VALIDATE_CREDENTIALS (node_modules/aws-sdk/lib/event_listeners.js:80:26)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at Request.runTo (node_modules/aws-sdk/lib/request.js:403:15)
at Request.send (node_modules/aws-sdk/lib/request.js:367:10)
at features.constructor.makeRequest (node_modules/aws-sdk/lib/service.js:193:27)
at features.constructor.svc.(anonymous function) [as describeTable] (node_modules/aws-sdk/lib/service.js:499:23)
at Table.describe (node_modules/dynamoose/lib/Table.js:339:7)
at Table.init (node_modules/dynamoose/lib/Table.js:141:10)
at Function.compile (node_modules/dynamoose/lib/Model.js:201:9)
at Dynamoose.model (node_modules/dynamoose/lib/index.js:39:21)
at Object.<anonymous> (models/modelName.js:63:35)
at require (internal/module.js:11:18)
at Object.<anonymous> (test/testFileName.js:4:25)
at require (internal/module.js:11:18)
at Array.forEach (<anonymous>)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
现在我知道这是一个问题,因为 dynamoose 无法配置区域。但是当我在开发中运行它时,它可以在同一段代码中正常工作。在我的配置文件中,我也有相同的数据库配置。我通过console.log(config.db)
在 if中添加来验证这一点(config.env == "test")
。所以我认为它应该配置。
如果需要,这是我的测试:
describe('ModelName', () => {
beforeEach((done) => {
ModelName.delete({}, (err) => {
done();
});
});
describe('/GET modelName', () => {
it('it should GET all the model-name instances', (done) => {
chai.request(server)
.get('/model-name')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
res.body.length.should.be.eql(0);
done();
});
});
});
});
任何想法可能是什么问题?