所以我有一个我试图测试的模型。它具有自定义端点以及标准REST端点。我建立了一个套件来测试标准操作。首先我贴了,然后我upserted(一个没有id ...基本上是一个帖子),然后我得到了。最后我删除了我发布时创建的条目,然后我自己清理后放入一个钩子来删除我已经发布的点。这很有效,很好。
然后,在同一个文件中我添加了另一个套件,这个套件测试我的自定义端点。现在我应该删除upserted点的钩子失败了。这不好。我尝试在新的describe语句中包装我的测试以进行删除,但是它再次无效。最终我完全没有使用钩子,只是发送了2个.del调用,它仍然无法正常工作。我的智慧结束了。如果你能弄清楚为什么它不起作用以及如何使它工作,请告诉我。提前谢谢!
这是我的代码:
var request = require('supertest');
var app = require('../server');
var chai = require('chai');
var chance = require('chance').Chance();
var assert= require('assert');
function json(verb, url) {
return request(app)[verb](url)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.expect('Content-Type', /json/);
}
describe('Testing standard REST endpoints-(CREATE, GET,PUT, DELETE)', function() {
//This suite tests the CREATE functionality
var ptOneID;
var lat1 =chance.latitude({fixed: 7, min: 41, max: 42});
var lng1 = chance.longitude({fixed:7, min: -88, max:-87});
it('should post a new point', function(done) {
var pt1 = new app.loopback.GeoPoint({lat: lat1, lng: lng1});
json('post', '/api/Points')
.send({
location: pt1,
})
.expect(200)
.end(function(err, res) {
ptOneID= res.body.id;
done();
});
});
//This test preforms a put with out an id-- an upsert
var lat5 =chance.latitude({fixed: 7, min: 41, max: 42});
var lng5 = chance.longitude({fixed:7, min: -88, max:-87});
var ptTwoID
it('should preform an UPSERT since were not specifying ID', function(done) {
var pt5 = new app.loopback.GeoPoint({lat: lat5, lng: lng5});
json( 'put','/api/Points/' )
.send({
location: pt5,
})
.end(function(err, res) {
var updatedPoint = res.body;
ptTwoID = updatedPoint.id;
done();
})
});
it('should return a list of all points', function(done) {
json('get', '/api/Points')
.expect(200)
.end(function(err, res) {
var points = res.body;
assert(Array.isArray(points));
assert.equal(points.length, 2, 'array length incorrect');
done();
});
});
//originally I didnt have the delete tests wrapped in a describe I added it to see if it changed anything
describe('DELETE has its own suite', function() {
//Delete the point
it('should DELETE the Point created by our POST test', function(done){
json('del', '/api/Points/' + ptOneID)
.end(function(err, res) {
assert.equal(res.status, 204, 'delete failed');
done();
})
});
it('should DELETE the Point we upserted',function(done){
json('del', '/api/Points'+ ptTwoID)
.end(function(err, res) {
assert.equal(res.status, 204, 'upsert delete failed');
done();
});
});
it('testing to see if posted point is still there', function(done){
json('get', '/api/Points/' + ptOneID)
.end(function(err, res) {
assert.equal(res.status, 404, 'posted point not deleted unsure emoticon');
done();
})
});
//this part was originally wrapped in an after hook, and worked perfectly until I added the describe for custom endpoints
it('testing to see if upserted point is still there', function(done){
json('get', '/api/Points/' + ptTwoID)
.end(function(err, res) {
assert.equal(res.status, 404, 'upserted pt not deleted unsure emoticon');
done();
})
});
});
});
//This suite tests the beforeSave behavior
describe('Testing Custom endpt', function() {
blah blah blh
});
最后一个有趣的事情:当我尝试进行upsert删除时,删除返回404,意味着找不到,但是get返回200状态意味着它已成功找到。请让我知道这里发生了什么。