javascript - 返回数组中的特定对象值

标签 javascript arrays unit-testing object methods

我想在测试期间检查特定的控制台日志

it('should return logs', function(done) {
  browser
      .log('browser').then(function(logs){
        console.log(logs);
      })
  ...
});

我得到的是:

[ { level: 'INFO',
    message: 'https://localhost:3000/ 42:14 "foo"',
    timestamp: 1485785320222 },
  { level: 'INFO',
    message: 'https://localhost:3000/ 43:14 "bar"',
    timestamp: 1485785320222 },
  { level: 'INFO',
    message: 'https://localhost:3000/ 46:14 Array[6]',
    timestamp: 1485785320225 },
  { level: 'SEVERE',
    message: 'https://localhost:3000/favicon.ico - Failed to load resource: the server responded with a status of 404 (Not Found)',
    timestamp: 1485785320298 } ]

所以它应该只返回一个日志条目列表,例如“foo”和“bar”,这样我就可以检查它是否等于我的假设值。

我现在不确定是否需要“for..in”来构建我可以重用的方法,或者只是循环遍历多个对象并查找logs.value.message并用“” trim 部分。

最佳答案

仅过滤与您要查找的内容匹配的日志可能是最简单/最容易理解的:

it('should return logs', function(done) {
  browser
      .log('browser').then(function(logs){
        console.log(logs.filter(function (logItem) {
          return logItem.message.match(/"foo"|"bar"$/);
        });
      })
  ...
});

关于javascript - 返回数组中的特定对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41939014/

相关文章:

unit-testing - 如何初始化一个对象并将其用于所有测试用例

javascript - 将函数应用于填充数字的对象

C++ & 位于新数组前面

JavaScript 求和多维数组元素值

Javascript过滤器检查多个值?

python - 用于多个测试的单元测试 setUp/tearDown

javascript - 在 Javascript 中添加 +GMT

javascript - 水平滚动到容器内的下一个元素会阻止标准滚动

javascript - 在 IE8 中,并非每次都能获取对嵌套框架对象的引用。添加alert()时它会起作用!

Android AsyncTask 和 Mockito(或 PowreMockito)