Ask Your Question
0

How can Golang dependency injection be used to incorporate a mock database in unit testing?

asked 2023-05-15 19:51:29 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-15 19:55:01 +0000

bukephalos gravatar image
  1. Define the Interface of the Database

The first step in using dependency injection in Golang is to define an interface for the database. This interface will be implemented by the actual database as well as the mock database.

type Database interface { GetUserById(string) (User, error) SaveUser(User) error }

  1. Implement the Database

Next, implement the actual database. For example, you might use MongoDB or PostgreSQL. Implement the methods defined in the interface you created in step 1.

type MongoDB struct { ... }

func (db MongoDB) GetUserById(id string) (User, error) { ... }

func (db *MongoDB) SaveUser(user *User) error { ... }

  1. Implement the Mock Database

Now, create a mock database by implementing the database interface. The mock database should return predetermined values for the methods in the interface.

type MockDatabase struct { ... }

func (db MockDatabase) GetUserById(id string) (User, error) { return &User{"mockFirstName", "mockLastName", 30}, nil }

func (db *MockDatabase) SaveUser(user *User) error { return nil }

  1. Instantiate the Database

In your code, create an instance of the database interface. In production, you would create an instance of the actual database. In unit tests, you would create an instance of the mock database.

func main() { db := &MongoDB{} ... }

func TestGetUserById(t *testing.T) { db := &MockDatabase{} ... }

  1. Pass the Database as a Dependency

Finally, pass the database instance created in step 4 as a dependency to the function or method being tested.

func GetUserById(db Database, id string) (*User, error) { ... }

In production, pass the actual database instance. In unit tests, pass the mock database instance.

user, err := GetUserById(db, "123") // user will be the mock user defined in the mock database implementation.

With this approach, you can easily swap out the database implementation with a mock database for unit testing.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-15 19:51:29 +0000

Seen: 10 times

Last updated: May 15 '23