node mongoose mocha如何调试失败的猫鼬测试?

时间:2017-05-02 22:03:32

标签: node.js mongoose mocha

与我之前使用Permissions所做的类似,我正在尝试测试将权限添加到我的用户...参数是正常的但是findOneAndUpdate推送不会将其插入到数组中...

MODEL

    const Schema = mongoose.Schema;

    const Privilege = new Schema({
      privilege: {
        table_id: {
          type: String,
          required: false
        },
        canWrite: {
          type: Boolean,
          required: true,
          default: false
        }
      }
    });

    const UserSchema = new mongoose.Schema({
      username: {
          type: String,
          required: true
      },
      ...
      privileges: [Privilege]
      ...

TEST

      const newWritePrivilege = { privilege: { user_id: '56z787zzz67fc', canWrite: true }};

     .post(`/api/v1/users/${user._id}/privileges`)
             .send(newWritePrivilege)
             .expect(httpStatus.OK)

CONTROLLER

    function grantPrivilege(req, res, next) {
      console.log('GRANT PRIVILEGE: %j TO USER %j', req.body, req.params.userId);
      User.findOneAndUpdate({ _id: req.params.userId }, { $push: { privilege: req.body } }, { new: true })
        .then(savedUser => res.json(savedUser))
        .catch((e) => {
          console.log('ERROR: ', e);
          next(e);
        });
    }

在console.log中,我可以检查用户ID是否正确,用户文档是否存在,但没有插入任何内容...... 我该如何调试呢?

CONSOLE

    GRANT PRIVILEGE: {"privilege":{"table_id":"56z787zzz67fc","canWrite":true}} TO USER "5908fe55aec3be291bc317b5"
    user: 
    {"__v":0,"username":"KK123","password":"KKpassword","email":"kk123@example.com","mobileNumber":"9999999999",
    "_id":"5908fe55aec3be291bc317b5","createdAt":"2017-05-02T21:47:01.795Z","privileges":[]}

感谢您的反馈

附加信息..

测试返回200但没有任何内容进入数组

    res:     
         {"req"{"method":"POST","url":"http://127.0.0.1:65296/api/v1/users/5909015a9be435292a992bfc/privileges",
   "data":{"privilege":
  {"table_id":"56z787zzz67fc","canWrite":true}},"headers":{"user-agent":"node-superagent/3.5.2",
   "content-type":"application/json"}},"header":{"vary":"X-HTTP-Method-Override, Accept-Encoding","x-dns-prefetch-control":"off","x-frame-options":"SAMEORIGIN","x-download-options":"noopen","x-content-type-options":"nosniff","x-xss-protection":"1; mode=block","access-control-allow-origin":"*",
  "content-type":"application/json; charset=utf-8","content-length":"4","etag":"W/\"4-K+iMpCQsduglOsYkdIUQZQMtaDM\"",
  "date":"Tue, 02 May 2017 21:59:54 GMT","connection":"close"},
  "status":200,"text":"null"}

1 个答案:

答案 0 :(得分:0)

架构:

**privileges**: [Privilege]

更新文件:

{ $push: { **privilege**: req.body } }

复数与单数。