注意:我还在Waterline上打开了一个https://github.com/balderdashy/waterline/issues/617,但没有得到比我的解决方案更简单的解决方案。
我有一个具有所有默认属性(例如createdDate、modifiedDate)的用户模型和一些自定义属性,如emailAddress、password (在toJSON()中被抑制)等,以及与其他模型的一些附加关联。
将由JSON格式的用户模型实例组成的控制器响应与Waterline模型实例进行比较的正确方法是什么?
例如,这个解决方法可以:
request
.get('/user/1')
.end(function (err, res) {
expect(res.body).to.deep.equal(
JSON.parse(JSON.stringify( // Workaround for toJSON not stringifying dates
_.cloneDeep( // Workaround for associations being lost
testUser.toJSON() // `testUser` is a Waterline model instance that should be equivalent to the requested data (`/user/1`)
)
))
);
done();
});但是几乎所有我尝试过的东西都没有;要么是关联丢失了( cloneDeep()解决了这个问题),要么是将字符串化的日期与日期对象进行了比较( JSON.parse(JSON.stringify())解决了这一点)。
是否有更好的方法将JSON与Waterline模型实例进行比较?也许是内置的公用设施?
发布于 2014-08-27 23:55:35
使用JSON模式怎么样?
https://stackoverflow.com/questions/25538641
复制相似问题