我在摩卡做了以下测试,产生了“未定义的AssertionError:未定义的==‘Ernest”。我隐约怀疑,测试实际上是在找到测试顶部创建的歌曲实例,我相信这证明了这一点。尽管如此,我不确定如何修复它。
这是为一个平均堆栈应用程序编写的api,其中猫鼬是ODM。
test.js
it('can save a song', function(done) {
Song.create({ title: 'saveASong' }, function(error, doc) {
assert.ifError(error);
var url = URL_ROOT + '/create/song/saveASong';
superagent.
put(url).
send({
title: 'saveASong',
createdBy: 'Ernest'
}).
end(function(error, res) {
assert.ifError(error);
assert.equal(res.status, status.OK);
Song.findOne({}, function(error, song) {
assert.ifError(error);
assert.equal(song.title, 'saveASong');
assert.equal(song.createdBy, 'Ernest');
done();
});
});
});
});
我的路线:
//PUT (save/update) song from the create view
api.put('/create/song/:title', wagner.invoke(function(Song) {
return function(req, res) {
Song.findOne({ title: req.params.title}, function(error, song) {
if(error) {
return res.
status(status.INTERNAL_SERVER_ERROR).
json({ error: error.toString() });
}
song.save(function(error, song) {
if(error) {
return res.
status(status.INTERNAL_SERVER_ERROR).
json({ error: error.toString() });
}
return res.json({ song: song });
});
});
};
}));
更新:我在"end“之后添加了一个console.log(res.body),它不包括"createdBy: Ernest”k/v对。因此,我试图修改被发送到另一个k/v对(当然是模式)的对象,但是仍然没有任何东西持续存在。我不会收到任何错误,如果我注释掉“断言.相等.‘欧内斯特”行。
我的最新版本的PUT路由:
api.put('/create/song/:title', wagner.invoke(function(Song) {
return function(req, res) {
Song.findOneAndUpdate({ title: req.params.title}, req.body ,{ new: true }, function(error, song) {
if(error) {
return res.
status(status.INTERNAL_SERVER_ERROR).
json({ error: error.toString() });
}
return res.json({ song: song });
});
};
}));
发布于 2016-03-25 13:53:48
以下api路由
api.put('/create/song/:title', wagner.invoke(function(Song) {
return function(req, res) {
Song.findOneAndUpdate({ title: req.params.title}, req.body ,{ new: true }, function(error, song) {
if(error) {
return res.
status(status.INTERNAL_SERVER_ERROR).
json({ error: error.toString() });
}
return res.json({ song: song });
});
};
}));
通过以下摩卡测试
it('can save a song', function(done) {
Song.create({ title: 'saveASong' }, function(error, doc) {
assert.ifError(error);
var url = URL_ROOT + '/create/song/saveASong';
superagent.
put(url).
send({
sections:[{ name: 'intro', timeSig: 140 }]
}).
end(function(error, res) {
assert.ifError(error);
assert.equal(res.status, status.OK);
console.log(res.body);
Song.findOne({}, function(error, song) {
assert.ifError(error);
assert.equal(song.title, 'saveASong');
//console.log(song);
assert.equal(song.sections[0].name, 'intro');
done();
});
});
});
});
https://stackoverflow.com/questions/35976646
复制相似问题