I was writing a DB test (runned by Mocha), calling findOne after saving a document and found a weird behavior:
pseudocode here: ...
test1
A.save(check('A',done));
test2
B.save(check('B',done));
test3
C.save(check('C',done));
...
check = function(name, done) {
theModel.findOne({name:name}, function(err,result) {
assert.notEqual(result,null);
result.remove(done);
});
}
Then, the A test passes BUT the B test doesnt pass. I check the logs and I find a curious thing: first its performed a insert, after a query, after a remove (for the first test, ok thats the expected behaviour). After the first test, I got shocked when I saw the query was performed BEFORE the insert (and so the test failed and nothing got removed). The third and so on behaves the same way! query is performed before insert :(
So the only test which passes is the first one (if I change A by B, then B is passed and A isnt). If I look at the mongodb collection I can see the other inserts which were performed after the query (and since assert failed, they werent removed)
I'm using mongoose 2.7.2, (but i was using a previous version, just updated to see if it was a solved bug). Help :(
I was wrong...
Didnt notice I was actually calling check before calling save (since I was calling the function inside the save method ... im so stupid and new to javascript).
Ok so I should perform the check in other way:
A.save(function(err, result) { check('A', done);})
my apologizes!