You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 from inversify-express-utils I'm failing trying to do:
Test
import{expect}from"chai"import{container}from'../../src/container/inversify.config'import{HealthControllerV1}from"../../src/controllers/v1/HealthControllerV1"import{results}from"inversify-express-utils"describe("ExampleController",()=>{letcontroller: HealthControllerV1beforeEach(()=>{controller=container.get<HealthControllerV1>(HealthControllerV1)})describe("Health",()=>{it("should return 200 status with Json response",async()=>{constresponse=controller.health()expect(response).to.be.an.instanceof(results.JsonResult)})})})
HealthControllerV1
Health
1) "before each" hook for "should return 200 status with Json response"
0 passing (8ms)
1 failing
1) HealthControllerV1
"before each" hook for "should return 200 status with Json response":
Error: No matching bindings found for serviceIdentifier: Symbol(HttpContext)
Error: No matching bindings found for serviceIdentifier: Symbol(HttpContext)
at _validateActiveBindingCount (node_modules/inversify/lib/planning/planner.js:63:23)
at _getActiveBindings (node_modules/inversify/lib/planning/planner.js:49:5)
at _createSubRequests (node_modules/inversify/lib/planning/planner.js:92:26)
at /home/marcosdipaolo/Documents/dev/goals-backend/node_modules/inversify/lib/planning/planner.js:116:17
at Array.forEach (<anonymous>)
at /home/marcosdipaolo/Documents/dev/goals-backend/node_modules/inversify/lib/planning/planner.js:115:26
at Array.forEach (<anonymous>)
at _createSubRequests (node_modules/inversify/lib/planning/planner.js:95:20)
at Object.plan (node_modules/inversify/lib/planning/planner.js:137:9)
at /home/marcosdipaolo/Documents/dev/goals-backend/node_modules/inversify/lib/container/container.js:319:37
at Container._get (node_modules/inversify/lib/container/container.js:312:44)
at Container.get (node_modules/inversify/lib/container/container.js:232:21)
at Context.<anonymous> (test/unit/health.test.ts:10:32)
at processImmediate (internal/timers.js:461:21)
thanks a lot
The text was updated successfully, but these errors were encountered:
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
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",()=>{letcontroller: HealthControllerV1container.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()=>{constresponse=controller.health()expect(response).to.be.an.instanceof(results.JsonResult)})})})
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: