我使用动态凝胶来扫描我的dynamoDB表上的孤立数据/僵尸数据,下面是示例。
var Foo=
dynogels.define('Account', {
hashKey : 'email',
// add the timestamp attributes (updatedAt, createdAt)
timestamps : true,
schema : {
email : Joi.string().email(),
account : Joi.string(), ---> this account is the id in Account table
age : Joi.number(),
roles : dynogels.types.stringSet(),
settings : {
nickname : Joi.string(),
acceptedTerms : Joi.boolean().default(false)
}
}
});
var Account = dynogels.define('Account', {
hashKey : 'email',
// add the timestamp attributes (updatedAt, createdAt)
timestamps : true,
schema : {
email : Joi.string().email(),
name : Joi.string(),
id : Joi.number(),
roles : dynogels.types.stringSet(),
settings : {
nickname : Joi.string(),
acceptedTerms : Joi.boolean().default(false)
}
}
});
function scanAccount(){
if(!attributeName && !FooList[attribute.id]){
return Account.scan().execAsync();
}
}
逻辑是,如果Foo表没有这个帐户,那么这个帐户就是孤立数据。我应该为这个函数写单元测试吗?或者我应该只写一个函数来查询Account表中的那些id,以测试它们是否存在?如果我编写单元测试,那么我仍然需要查询dynamoDB来检查值是否正确,因为我有比1000+更多的东西来检查这些帐户id是否在Foo Table中不存在,但我不确定这是否仍然是进行单元测试的方法?
发布于 2018-05-13 05:42:40
为了清楚起见,为scanAccount方法编写单元测试看起来像这样(在伪代码中):
function when_attributeName_and_notFooHasAttribute_and_accountHasData_returnData() {
// setup mock Account
// setup scan().execAsync() on mockAccount to return some test data
// set attributeName = 'TEST-NAME'
// setup mock empty FooList
// invoke scanAccount()
// assert that you received data as expected
}
function when_notAttributeName_returnUndefined() {
// setup mock Account
// setup scan().execAsync() on mockAccount to return some test data
// set attributeName = undefined
// setup mock empty FooList
// invoke scanAccount()
// assert that the result is undefined
}
function when_attributeName_and_FooHasAttribute_returnUndefined() {
// setup mock Account
// setup scan().execAsync() on mockAccount to return some test data
// set attributeName = 'TEST'
// setup FooList to contain attribute.id
// invoke scanAccount()
// assert that the result is undefined
}编写这样的测试是有价值的,但不要将单元测试误认为集成测试。在编写单元测试时,您不会测试数据的检索,而只是测试scanAccount实现的逻辑是否正确。
顺便说一句,我不确定该方法的确切意图是什么,但它看起来有一些错误。不管怎样,只要想想你应该写什么测试就可以帮助你找到bug。编写测试将使这一点变得明确,而且它肯定会在未来帮助您解决回归问题!
https://stackoverflow.com/questions/50290442
复制相似问题