-
Notifications
You must be signed in to change notification settings - Fork 98
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
Error trying to instantiate controller in a test (Error: No matching bindings found for serviceIdentifier: Symbol(HttpContext)) #360
Comments
marcosdipaoloSV
changed the title
Error trying to instantiate controller in a test (extending BaseHttpController, using inversify-express-utils)
Error trying to instantiate controller in a test (Error: No matching bindings found for serviceIdentifier: Symbol(HttpContext))
Feb 25, 2021
For some reason i neither know nor understand, in a test you need to mock the httpContext (the one you get from BaseHttpController) and bind it to the container before you try to instantiate the controller The mocked httpContext import { mockReq, mockRes } from 'sinon-express-mock'
import { interfaces } from "inversify-express-utils"
import { container } from '../src/container/inversify.config'
const mockRequest = mockReq()
const mockResponse = mockRes()
const mockUser: interfaces.Principal = {
details: "",
isAuthenticated: () => Promise.resolve(true),
isResourceOwner: () => Promise.resolve(true),
isInRole: () => Promise.resolve(true)
}
export const mockedHttpContext: interfaces.HttpContext = {
request: mockRequest,
response: mockResponse,
user: mockUser,
container
} The test import { expect } from "chai"
import { container } from '../../src/container/inversify.config'
import { HealthControllerV1 } from "../../src/controllers/v1/HealthControllerV1"
import { results, interfaces } from "inversify-express-utils"
import { mockedHttpContext } from '../mockedHttpContext'
describe("HealthControllerV1", () => {
let controller: HealthControllerV1
container.bind<interfaces.HttpContext>(Symbol.for("HttpContext")).toConstantValue(mockedHttpContext)
beforeEach(() => {
controller = container.get<HealthControllerV1>(HealthControllerV1)
})
describe("Health", () => {
it("should return 200 status with Json response", async () => {
const response = controller.health()
expect(response).to.be.an.instanceof(results.JsonResult)
})
})
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to write a unit test for a controller that has an injected service at the constructor, that's why (unlike the docs where controllers are instantiated with the
new
keyword in a test) i need to instantiate it with the container.The controller extends the
BaseHttpController
frominversify-express-utils
I'm failing trying to do:Test
Container:
Controller:
Error:
My Environment
Stack trace
thanks a lot
The text was updated successfully, but these errors were encountered: