-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expect(spy).to.have.been.called.with always true #82
Comments
I think you need to return the it('test SQL', () => {
Item.database = { query: queryReturnsInsert };
const item = new Item();
item.name = 'James';
const spy = chai.spy.on(Item.database, 'query');
return item.save({ in: 'go' })
.then(function() {
expect(spy).to.have.been.called.with(null);
});
}); |
@vieiralucas You are right, but now I get this:
After changing it the last code to what should return it('test SQL',
() => {
Item.database = { query: queryReturnsInsert };
const item = new Item();
item.name = 'James';
const spy = chai.spy.on(Item.database, 'query');
return item.save({ in: 'go' })
.then(function() {
expect(spy).to.have.been.called.with(`INSERT INTO items
(\`name\`)
VALUES(?)`, 'go', [item.name]);
});
}); |
@xavicolomer are you sure that |
Yes it is called. Please find below an extended version of the code above with the function. The queryReturnsInsert is logged correctly. I also tried adding eventually (although since its inside then its already resolved) it('test SQL',
() => {
const queryReturnsInsert = (arg1, arg2, arg3) => {
console.log('Query arguments: ', arg1, arg2, arg3);
return Promise.resolve({ insertId: 0 })
};
Item.database = { query: queryReturnsInsert };
const item = new Item();
item.name = 'James';
const spy = chai.spy.on(Item.database, 'query');
return item.save({ in: 'go' })
.then(function() {
expect(spy).to.have.been.called.with(`INSERT INTO items
(\`name\`)
VALUES(?)`, 'go', [item.name]);
});
}); then I get this error: TypeError: { [Function]
toString: [Function: toString],
reset: [Function],
__spy: { calls: [ [Object] ], called: true, name: undefined } } is not a thenable. |
Since the database.query its abstract from the ORM, I can also simply kind of mock and save the arguments: let _sql, _country, _values;
const queryReturnsInsert = (SQL, country, values) => {
_sql = SQL;
_country = country;
_values = values;
return Promise.resolve({ insertId: 0 })
}; And then: expect(_sql).to.equal(`INSERT INTO items
(\`custom_name\`)
VALUES(?)`); Although its not really clean |
In order to check precise order of arguments you need to use |
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"chai-spies": "^0.7.1",
Hi there,
I am testing a little ORM I created myself
I am having some problems testing the result of a spy on a promise, the test always passes, no matter what I put inside with, in the example below I just put null, although query should be called with a SELECT SQL sentence
Any suggestions?
The text was updated successfully, but these errors were encountered: