go-sqlmock
mockery
go-sqlmock | mockery | |
---|---|---|
20 | 20 | |
6,088 | 6,011 | |
1.1% | 1.3% | |
4.9 | 8.1 | |
25 days ago | 3 days ago | |
Go | Go | |
GNU General Public License v3.0 or later | BSD 3-clause "New" or "Revised" License |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
go-sqlmock
- Princípios SOLID em GoLang - Dependency Inversion Principle (DIP)
- How do you unit-test code that reaches out to the db, without introducing interfaces everywhere?
-
Creating an API using Go and sqlc
For that, I used the lib go-sqlmock. So, for example, the following snippet is part of the person/service_test.go file:
-
Using SQLC in project how do I mock database Calls with it for unit testing?
It's not the right call IMO to skip mocking the database connection to achieve 100% test coverage. How your app will behave in failure scenarios that are impossible to imitate during integration tests is part of the software contract. If your choice is to panic, or return an error, document that by testing that behavior. If another dev, or future you inadvertently breaks the contract, the test suite will fail. That's what you want. For unit tests against your database you should be using either go-sqlmock if testing against database/sql or pgxmock if testing against pgx. That being said, the points raised elsewhere in this thread regarding unit tests potentially hiding edge cases in terms of how an actual database will interact with your application that are not reflective of your understanding when writing mocks are 100% valid. You should do both. Unit test your app and write integration tests as well. On my team, we run integration tests using docker-compose.
- What is the coolest Go open source projects you have seen?
- How to mock database calls
-
Can you set expectations for SQL transaction using Testify?
I use Sqlmock for that purpose
- Mocking database queries - ask for opinion
- SQL mock driver for Golang to test database interactions
- Can't get a specifc SQL query with pgx to work
mockery
-
I Write Tests in Go
I'm far too lazy to write mocks by hand in go. You can generate a mock for a given interface with mockery https://github.com/vektra/mockery
-
go-ecommerce-microservices: A practical e-commerce microservices, built with cqrs, event sourcing, vertical slice architecture, event-driven architecture.
Some of the features: - ✅ Using Vertical Slice Architecture as a high level architecture - ✅ Using Event Driven Architecture on top of RabbitMQ Message Broker with a custom [Event Bus](pkg/messaging/bus/) - ✅ Using Event Sourcing in Audit Based services like [Orders Service](services/orders/) - ✅ Using CQRS Pattern and Mediator Patternon top of Go-MediatR library - ✅ Using Dependency Injection and Inversion of Controlon top of uber-go/fx library - ✅ Using RESTFul api with Echo framework and using swagger with swaggo/swag library - ✅ Using Postgres and EventStoreDB to write databases with fully supports transactions(ACID) - ✅ Using MongoDB and Elastic Search for read databases (NOSQL) - ✅ Using OpenTelemetry for collection Distributed Tracing with using Jaeger and Zipkin - ✅ Using OpenTelemetry for collection Metrics with using Prometheus and Grafana - ✅ Using Unit Test for testing small units with mocking dependent classes and using Mockery for mocking dependencies - ✅ Using End2End Test and Integration Test for testing features with all of their real dependeinces using docker containers (cleanup tests) and testcontainers-go library
- Direction of mockery: absorb different styles of mocks into this project · vektra mockery · Discussion #671
-
Go: Write Mock Test cases using Mockery and
Read mockery documentation: https://vektra.github.io/mockery/
-
Mocking unexported interfaces?
https://github.com/vektra/mockery is the better mocking framework in my biased opinion. The Google maintainers for gomock seem to have neglected the project.
-
I want to contribute to open source but don't know where to start
There are some one liner changes you can implement in https://github.com/vektra/mockery
-
Is gomock still maintained and recommended?
When there's just one heavyweight dependency you're interacting with, perhaps a one-off stub/fake is simpler, but I would posit that auto-generated mocks via things like mockery + go:generate leave less test code to maintain vs. perhaps many stubs across the project.
-
vektra/mockery: v2.21.0 includes "packages" config section, allowing 5x faster mock generation and more fine-grained control over mocks
Please take a look at the documentation linked here, and the related discussion topic that spawned this.
-
Issue implementing interface method that returns another interface
As you can see, the package has a chained method Logical().Write() . Since I want to create tests for PublicFunctionIWantToTest, I want to pass down all the functionality as interface so that I can use https://vektra.github.io/mockery/ to create mocks for it.
- FEEDBACK REQUESTED: Deprecation of automatic interface discovery · Discussion #549 · vektra/mockery
What are some alternatives?
gomock - GoMock is a mocking framework for the Go programming language.
go-txdb - Immutable transaction isolated sql driver for golang
counterfeiter - A tool for generating self-contained, type-safe test doubles in go
gock - HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽
minimock - Powerful mock generation tool for Go programming language
hoverfly - Lightweight service virtualization/ API simulation / API mocking tool for developers and testers
gotests - Automatically generate Go test boilerplate from your source code.
moq - Interface mocking tool for go generate
realize - Realize is the #1 Golang Task Runner which enhance your workflow by automating the most common tasks and using the best performing Golang live reloading.
webmock - Webmock for Golang, to stub requests.