【问题标题】:Commpering data from MongoDB in node.js在 node.js 中查看来自 MongoDB 的数据
【发布时间】:2017-04-03 15:55:44
【问题描述】:

首先,编码对我来说是新事物。到目前为止,我是 Java 应用程序的手动测试员。 现在,我的任务是学习做“自动化的事情”,但我遇到了一个问题。

我正在尝试从 MongoDB 获取数据并将其与测试数据进行比较。测试是量角器和黄瓜的混合体。

我认为问题在于我不理解或我还不知道。

在 mongoDB 我有:

{
    "_id" : 123456,
    "mykey" : 2
}

IDE 中的代码:

import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
const expect = chai.expect;

module.exports = function () {
    this.Then(/^Data should be same$/, function (callback) {

// Connection to mongoDB

    const MongoClient = require('mongodb').MongoClient;
    MongoClient.connect('mongodb://localhost:8517/fpl').then(function(db) {

      const collection = db.collection('test');

// Test data

      const testData = [{
        _id: 123456,
        mykey: 2
      }];

// Query to DB for data

      const a = collection.find({_id: 123456}).toArray();

// "is data from DB == test data?"

      return expect(a).to.eventually.equal(testData).notify(callback);


    }).catch((err) => {
      console.log(err);
});

结果我收到了消息:

Message:
 AssertionError: expected [ { _id: 123456, mykey: 2 } ] to equal [ { _id: 123456, mykey: 2 } ]

1 scenario (1 failed)
1 step (1 failed)
0m00.094s
AssertionError: expected [ { _id: 123456, mykey: 2 } ] to equal [ { _id: 123456, mykey: 2 } ]
  message: 'expected [ { _id: 123456, mykey: 2 } ] to equal [ { _id: 123456, mykey: 2 } ]',
  showDiff: true,
  actual: [ { _id: 123456, mykey: 2 } ],
  expected: [ { _id: 123456, mykey: 2 } ] }
[14:22:34] I/local - Shutting down selenium standalone server.
[14:22:34] I/launcher - 0 instance(s) of WebDriver still running
[14:22:34] I/launcher - chrome #01 failed 1 test(s)
[14:22:34] I/launcher - overall: 1 failed spec(s)
[14:22:34] E/launcher - Process exited with error code 1

任何想法我做错了什么?

【问题讨论】:

    标签: node.js mongodb typescript protractor cucumber


    【解决方案1】:

    使用deep 标志 http://chaijs.com/api/bdd/#method_deep

    collection.find({_id: 123456}).toArray(function(err, docs) {
        expect(docs).to.deep.equal(testData).notify(callback);
    });
    

    【讨论】:

    • 已更新,立即尝试
    • 仍然错误 [15:34:48] E/launcher - [ { _id: 123456, mykey: 2 } ] 不是 thenable。 [15:34:48] E/launcher - TypeError: [ { _id: 123456, mykey: 2 } ] 不是 thenable。我认为问题是来自 db 的数据类型,
    • 我忘了把eventually改成deep,抱歉:)
    • 我在第一个错误后尝试“深”,仍然是“不是一个 thenable”
    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 2011-10-26
    • 2014-01-18
    • 2020-12-04
    • 2021-11-02
    • 2016-12-14
    相关资源
    最近更新 更多