mocha api 测试 - PUT 路由
mocha api test - PUT route
我在 mocha 中进行了以下测试,结果为“Uncaught AssertionError: undefined == 'Ernest'。我暗暗怀疑该测试实际上是在寻找在测试顶部创建的歌曲实例,我相信这证明了这一点。话虽这么说,但我不确定如何解决它。
这是为 MEAN 堆栈应用程序编写的 api,使用 mongoose 作为 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 对(当然是来自模式)的对象,但仍然没有任何东西存在。如果我注释掉 "assert.equal...'Ernest'" 行,我不会收到任何错误。
我最新版本的 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 });
});
};
}));
以下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 });
});
};
}));
通过以下 mocha 测试
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();
});
});
});
});
我在 mocha 中进行了以下测试,结果为“Uncaught AssertionError: undefined == 'Ernest'。我暗暗怀疑该测试实际上是在寻找在测试顶部创建的歌曲实例,我相信这证明了这一点。话虽这么说,但我不确定如何解决它。
这是为 MEAN 堆栈应用程序编写的 api,使用 mongoose 作为 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 对(当然是来自模式)的对象,但仍然没有任何东西存在。如果我注释掉 "assert.equal...'Ernest'" 行,我不会收到任何错误。
我最新版本的 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 });
});
};
}));
以下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 });
});
};
}));
通过以下 mocha 测试
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();
});
});
});
});