-
Notifications
You must be signed in to change notification settings - Fork 14
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
stubConstructor does not stub base class methods #137
Comments
Is there some kind of work around for this? I guess an interface that defines all methods? Would this be a difficult thing to fix? I may be able to help. |
I'm having a similar problem. I haven't been able to find a way to stub parent methods in a child class. import express from 'express';
class BaseController {
error(res: express.Response, errorMessage: string, statusCode: number) {
res.status(statusCode).json(errorMessage)
}
}
class AuthController extends BaseController {
constructor() { super() }
verifyAuthToken(req, res, next) { // normal express.js style for controllers
const token = getToken() // imagine this was a function that returns some auth token
if (token === undefined) {
super.error(res, 'Auth token not provided', 400);
}
}
} I had some code similar to what's above and I wanted to write a test to make sure when the token is undefined, the error method on the parent class will be called. But I could not test that the error method was called: import { stubObject, stubInterface, stubConstructor } from "ts-sinon";
import { expect } from "chai";
const authServiceStub = stubObject<AuthService>(authService);
describe('AuthService', () => {
describe('verifyAuthToken method', () => {
it('should call super.error when no getToken returns undefined', () => {
....
authServiceStub.verifyAuthToken(req, res, next)
expect(authServiceStub.error.callCount).to.be.equal(1)
expect(authServiceStub.error.calledWith('Auth token not provided')).to.be.true
})
}) When I run the test, I get the following error: Please I really need help on this! Is there a specific way to create stubs for parent methods of a class? |
I guess the workaround might be to use stubInterface and manually stub all the methods you need for a specific test, however, this is not ideal. |
Have the same issue. Any ideas? |
@laleksiunas @Emmytobs @joshystuart I've dug into it a little, and found out that it works when I transform my typescript code to ES5. Everything above that doesn't work. It's connected with |
I've created a pull request which should resolve this issue. Please check it -> #251 |
Fix for class inheritence issue #137
Steps to reproduce:
Code sample:
The text was updated successfully, but these errors were encountered: