How should I write a unit test forJavaScript frontend service that uses promise with Karma, Mocha and Sinon -
trying test services i'm writing interact 3rd party api , wondering how test efficiently.
i have next method:
function getmemberprofile(memberid) { //make sure memberid defined , number if (!isnan(memberid)) { return client.authorizedapirequest('/members/' + memberid).get(); } return promise.reject(new error('proper memberid not supplied')); }
when client.authorizedapirequest('/members/' + memberid).get() calls 3rd party api , returns promise resolves object (i.e. {id:12,name:'john doe'}).
so, how should test getmemberprofile function? thinking mocking out client.authorizedapirequest("some params").get() sinon can't working.
thanks
ok, got working. first you'll need install chai. then, in spec file:
beforeeach(function () { fakemember = { member: { id: 10002, first_name: 'john', last_name: 'doe' } }; }); it('should member\'s profile memberid', function () { //mock sinon.stub(client, 'authorizedapirequest').withargs('/members/' + fakemember.member.id).returns({ get: function () { return promise.resolve(fakemember); } }); return members.getmemberprofile(fakemember.member.id).then(function (response) { expect(response).to.have.property('member'); expect(response.member).to.have.property('id', fakemember.member.id); expect(response.member).to.have.property('first_name', fakemember.member.first_name); expect(response.member).to.have.property('last_name', fakemember.member.last_name); }); });
Comments
Post a Comment