【问题标题】:How to write unit test for my function which I use to query dyanmoDB Table?如何为用于查询 dynamoDB 表的函数编写单元测试?
【发布时间】:2018-05-13 05:42:39
【问题描述】:

我使用 dynogels 扫描我的 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+ 来检查 Foo 表中是否不存在这些帐户 ID,但我不确定如果这仍然是进行单元测试的方式?

【问题讨论】:

    标签: javascript amazon-web-services unit-testing amazon-dynamodb


    【解决方案1】:

    明确地说,为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 实现的逻辑是正确的。

    顺便说一句,我不确定该方法的确切意图是什么,但它看起来有一些错误。无论哪种方式,只要考虑应该编写哪些测试就可以帮助您找到错误。编写测试将使这一点变得明确,并且肯定会帮助您将来进行回归!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 2016-09-25
      相关资源
      最近更新 更多