2017-02-02 19 views
5

Test çerçevesi olarak mocha kullanıyorum ve HTTP 204 durum kodu döndüren bir son noktaya karşı kullanan bir DELETE isteğini denemeye çalışıyorum.Nock isteği keser ama boş nesneyi döndürür

it('should logout user', (done) => { 
    nock(<domain>) 
    .log(console.log) 
    .delete(path) 
    .reply(204, { 
     status: 204, 
     message: 'This is a mocked response', 
    }); 

    api.logout(token) 
    .then((response) => { 
     console.log('IS DONE?--->', nock.isDone()); 
     console.log('RESPONSE--->', response); 
     done(); 
    }) 
    .catch((error) => { 
     console.log('ERROR--->', error); 
    }); 
}); 

Bu aşağıdaki çıktı döndürür::

İşte Test kodudur

matching <domain> to DELETE <domain>/<path>: true 
(the above line being generated by the .log method in nock) 
IS DONE?---> true 
RESPONSE---> {} 

Eğer log() ve isDone() nock yöntemlerle belirtildiği gibi istek düzgün ele ediliyor görebileceğiniz gibi

, ancak döndürülen response nesnesi boş bir nesnedir, bu nedenle döndürülen HTTP durum koduyla ilgili iddialar yapmak mümkün değildir (bu örnekte 204)

Burada nelerin eksik olabileceğine dair bir fikriniz var, neden reply() yöntemi boş bir nesneyi döndürüyor?

UPDATE Burada

logout yöntemi için kodu remove yöntem DELETE HTTP yöntemiyle bir fetch talebi için bir sarıcı.

logout(token) { 
    return remove(
    this.host, 
    END_POINTS.DELETE_TOKEN, 
    { 
     pathParams: { token }, 
    }, 
    { 
     Accept: 'application/json', 
     'Content-Type': 'application/json', 
     Authorization: `Bearer ${token}`, 
    }, 
); 
} 
+0

"api.logout" kodunun anlamı nedir? Bu, "nock" tarafından sağlanan bir şey gibi görünmüyor. Bana öyle geliyor ki orada bir hata olabilir. – Louis

+0

Son nokta için bir DELETEHTHT yöntemini temsil eder, iyi çalışır ve çıktıda gösterildiği gibi doğru şekilde kesilir gibi görünüyor, kodu dahil etmek için soruyu güncelledim, daha önce dahil etmedim karışıklığı önlemek. – rfc1484

cevap

1

bence 204 sen yanıtı geri dönebilirler 200'e tabii sunucuyu değiştirmek gerekebilir, böylece bir yanıt body olması gerekiyordu, ama onu işlemez Getirme düşünüyorum değildir. request gibi diğer paketler, gövdeyi 204 durumuyla işleyecek, ancak bu istek paketi yalnızca sunucu tarafı içindir.

Ayrıca, sarıcınızın ne yaptığından emin değilsiniz, ama yanıtı response.json() promise kullanarak almanız gerektiğini düşünüyorum. Ve mocha da vaatleri otomatik olarak halledebilir, onları geri alabilirsin.

const nock = require('nock') 
const fetch = require('isomorphic-fetch'); 
const request = require('request') 

const domain = "http://domain.com"; 
const path = '/some-path'; 
const token = 'some-token'; 

const api = { 
    logout: (token) => { 
     return fetch(domain + path, { 
      method: 'DELETE', 
      headers: { 
       'Content-Type': 'application/json' 
      } 
     }); 
    } 
} 

describe('something',() => { 
    it('should logout user with 204 response using request package', (done) => { 
     nock(domain) 
      .log(console.log) 
      .delete(path) 
      .reply(204, { 
       status: 204, 
       message: 'This is a mocked response', 
      }); 

     request.delete(domain + path, function(err, res) { 
      console.log(res.body); 
      done(err); 
     }) 
    }); 

    it('should logout user',() => { 
     nock(domain) 
      .log(console.log) 
      .delete(path) 
      .reply(200, { 
       status: 200, 
       message: 'This is a mocked response', 
      }); 

     return api.logout(token) 
      .then((response) => { 
       console.log('IS DONE?--->', nock.isDone()); 
       return response.json(); 
      }) 
      .then(function(body) { 
       console.log('BODY', body); 
      }) 
      .catch((error) => { 
       console.log('ERROR--->', error); 
      }); 
    }); 
}); 

Bu irade çıktı:

something 
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true 
{"status":204,"message":"This is a mocked response"} 
    ✓ should logout user with 204 response 
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true 
IS DONE?---> true 
BODY { status: 200, message: 'This is a mocked response' } 
    ✓ should logout user 

kullanılan-PS ardından: Ben yardımcı olur umarım

"dependencies": { 
    "isomorphic-fetch": "^2.2.1", 
    "mocha": "^3.2.0", 
    "nock": "^9.0.6", 
    "request": "^2.79.0" 
    } 

aşağıda tam bir örneğe bakın.

İlgili konular