samedi 28 février 2015

verifying contents of a resolved promise via function call with chai-as-promised

I'm using chai-as-promised in conjunction with sinonjs and I'm unable to work out how to verify a changed password.


I have something like the following code:



it('changes password if old password is valid', function(){

var u = new User({id: '1234567890', email:'user@email.com'});
var encrypted_password = bcrypt.hashSync('newtestPassword', bcrypt.genSaltSync(8), null);
sinon.stub(db, 'executeQuery', function() {
return q.fcall( function() {
return [ {u: { _data: { data: {flake_id: '1234567890', email:'user@email.com', encrypted_password: encrypted_password}}}}];
})

})

return u.ChangePassword('testPassword', 'newTestPassword').should
.eventually.be.an.instanceof(User)
.and.have.property('encrypted_password')
.that.satisfy(bcrypt.compareSync('newTestPassword', this));
});


my problem is the last line of the assertion - I want to be able to extract the encrypted password from the stubbed database response and user bcrypt.compareSync() to check that it is the correct value, but I'm unclear on how to reference the encrypted_password value from the response.


According the the docs, the property() call makes that property the subject of the assertion which is why I tried to use this but I end up with bcrypt throwing an 'Incorrect arguments' error which I presume is due to the this reference.


Does anyone have a suggestion about how to do this properly?


note: I do intend to replace the stub with a mock that checks the query.


Aucun commentaire:

Enregistrer un commentaire