首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为我用于查询dyanmoDB表的函数编写单元测试?

如何为我用于查询dyanmoDB表的函数编写单元测试?
EN

Stack Overflow用户
提问于 2018-05-11 18:36:52
回答 1查看 322关注 0票数 0

我使用动态凝胶来扫描我的dynamoDB表上的孤立数据/僵尸数据,下面是示例。

代码语言:javascript
复制
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中不存在,但我不确定这是否仍然是进行单元测试的方法?

EN

回答 1

Stack Overflow用户

发布于 2018-05-13 05:42:40

为了清楚起见,为scanAccount方法编写单元测试看起来像这样(在伪代码中):

代码语言:javascript
复制
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。编写测试将使这一点变得明确,而且它肯定会在未来帮助您解决回归问题!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50290442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档