question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test a controller with a service that has @Inject in its constructor?

See original GitHub issue

I have a question regarding testing as described on https://docs.nestjs.com/advanced/unit-testing:

beforeEach(async () => {
    const module = await Test.createTestingModule({
        controllers: [CatsController],
        components: [CatsService],
      }).compile();

    catsService = module.get<CatsService>(CatsService);
    catsController = module.get<CatsController>(CatsController);
});

The CatService is quite simple. How about this?

constructor(@Inject(CAT_MODEL_TOKEN) private readonly catModel: Model<Cat>) {}

How are we supposed to provide the catModel to CatsService in beforeEach? Without providing any parameters I get an error:

Nest can’t resolve dependencies of the CatsService. Please verify whether all of them are available in the current context.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:33 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
artworkadcommented, Nov 7, 2017

@adrien2p finally 💃 problem solved, thank you so much.

4reactions
univerzecommented, Sep 25, 2018

Not sure if it’s still relevant after a year, but when you are unit testing your controller, you don’t want to worry about it’s dependencies so you mock them out by doing:

class MockCatsService {
}
describe('CatsController', () => {

    beforeEach(async () => {
        catsController: CatsController;
        catsService: CatsService;

        const module = await Test.createTestingModule({
            controllers: [CatsController],
            providers: [
                { provide: CatsService, useClass: MockCatsService },
            ],
        }).compile();

        catsService = module.get<CatsService>(CatsService);
        catsController = module.get<CatsController>(CatsController);
    });

});

so when your CatsService changes you don’t have to update it in every single place you use it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - How to unit test Controller and mock @InjectModel in ...
I am testing my API using the @injectModel and another service. Here's the snippet: import { CategoriesService } from './.
Read more >
Testing your controllers when you have a decoupled core
We need a way to inject this mock into the controller as a constructor argument. This should only happen when the controller is...
Read more >
Dependency injection into controllers in ASP.NET Core
ASP.NET Core MVC controllers request dependencies explicitly via constructors. ASP.NET Core has built-in support for dependency injection (DI).
Read more >
Make services unit testable using dependency injection
We will use Mockito to provide and inject mock objects into WatchlistService . We will use these annotations:
Read more >
Spring Boot Unit Testing | Code With Arho
The solution is not to use field injection at all. Instead, we should use constructor injection: @Service public class OrderService ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found