スーパーテストとモカを使用して、ストロングループ/ループバック API のいくつかのテストに取り組んでいます。標準エンドポイントの 1 つは Model/update です。Update は実際にはPersistedModel.updateAllの形式であり、クエリを受け取り、クエリに一致するすべてのエントリに投稿します。これは、成功したリクエストが Explorer でどのように表示されるかを示した図です。
図から、リクエスト URL は主に単なるクエリ文字列であり、204 を返すことに注意してください。superagent のドキュメントから、投稿でクエリを送信できることがわかります。ただし、これをテストで複製するのに苦労しています。これが私のrequireステートメントです:
var request = require('supertest');
var app = require('../server');
var assert = require('chai').assert;
var chance = require('chance').Chance();
ここに私のテストがあります
describe('/api/Points/update', function(){
var updatedZip = "60000";
it('should grab a Point for a before reference', function(done) {
json('get', '/api/Points/' +addID )
.end(function(err, res) {
assert.equal(res.body.zipcode, addZip, 'unexpected value for zip');
done();
});
});
it('should update the Point w/ a new zipcode', function(done) {
var where = {"zipcode": "60035"};
var data ={"zipcode": updatedZip};
request(app)
.post('/api/Points/update')
.query({"where": {"zipcode": "60035"}})
.send({
data : data
})
.end(function(err, res) {
assert.equal(res.status, 204, 'update didnt take');
done();
});
});
it('should check to see that the Point was updated', function(done) {
json('get', '/api/Points/' +addID )
.end(function(err, res) {
assert.equal(res.body.zipcode, updatedZip, 'updated zip was not applied');
done();
});
});
最初のテストは成功しました。つまり、リクエストのステータスとして 204 が返されましたが、2 番目のテストで失敗したということは、クエリが受け入れられることがわかったにもかかわらず、実際には更新が適用されなかったことを意味します。さまざまな処方を試しましたが、どれも効果がありませんでした。これをシミュレートする方法を教えてください!よろしくお願いします。