Skip to content

How to mock dependencies? #1789

Answered by provinzkraut
kayprogrammer asked this question in Q&A
Discussion options

You must be logged in to vote

A couple of things here, but let me just say that this isn't something Litestar specific but rather about how scopes, imports and mocking works.

  1. You are misusing the application factory pattern. Having a create_app function and then immediately calling it in the same file is functionally equivalent to simply creating the app in the file directly.
  2. The issue you're seeing is because of the order of execution. What happens in your code is:
  3. You import app. This will also set the dependencies on that object
  4. You mock app.core.database.get_db. Since the application object has already been created, this will not change the get_db function you passed to it

To fix it you can either:

  1. Mock get_db

Replies: 9 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@provinzkraut
Comment options

Answer selected by provinzkraut
@kayprogrammer
Comment options

Comment options

You must be logged in to vote
1 reply
@euri10
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
Question This is a question and further information is requested
5 participants
Converted from issue

This discussion was converted from issue #1787 on June 07, 2023 10:07.