Skip to content

How to mock mongo with this interface? #30

Answered by SVilgelm
snavarro89 asked this question in Q&A
Discussion options

You must be logged in to vote

OK. Here is an example of how to mock the mongo objects

Let's say we have a function that returns all admin users:

package simple

import (
	"context"

	"go.mongodb.org/mongo-driver/bson/primitive"

	"github.com/sv-tools/mongoifc"
)

const (
	UsersCollection = "users"
)

type User struct {
	ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
	Name string `json:"name,omitempty" bson:"name,omitempty"`
	Email string `json:"email,omitempty" bson:"email,omitempty"`
	Active bool `json:"active,omitempty" bson:"active,omitempty"`
	IsAdmin bool `json:"is_admin,omitempty" bson:"is_admin,omitempty"`
}

func GetAdmins(ctx context.Context, db mongoifc.Database) ([]*User, error) {
	var users

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by SVilgelm
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
None yet
2 participants