A simple go library for generating mock data based on structs.
A minimal library that helps to generate structs with mock data when creating unit tests with libraries like "github.com/stretchr/testify/suite"
.
Creating mocked data is difficult and time consuming. This leads to a lot of boiler plate code resulting in brittle tests.
I wanted a faster way to define mocks based on constrains and structure instead of the exact content of all of the fields.
Here's a simple example.
You have a Person
struct to mock.
type Person struct {
FName string
Surname string
Age int
privateField int // This should be ignored
}
Simply pass the struct to salem.Mock
and then run Execute
on the factor that is created for your struct.
factory := salem.Mock(examples.Person{})
results := factory.Execute()
Sample Output for the example:
Salem mocks:
[
{
"FName": "BDMHKCTVZMER",
"Surname": "BGGCEPUWFSKWHGH",
"Age": 26
}
]
See the examples folder for more information.
-
Mock primities, structs, slices and maps automatically
-
Control the value of public fields that are mocked with
Ensure(...)
-
Control the specfic values of public fields with
EnsureSequence(...)
andEnsureSequenceAcross(...)
-
Apply constraints to values that are generated with
EnsureConstraint(...)
-
Control the number of mocks generated with
WithMinItems()
,WithMaxItems()
andWithExactItems()
-
Control nested public fields with path name e.g.
ChildField.NestedChild.OtherNestedChild
-
Omit fields with
Omit(...)
-
Mock nested fields automatically
-
Easily integrate with external APIs with via custom field handlers
OnField(...)
. -
Retain fined-grained control nested fields with
Tap()
-
For public map fields:
- Set the keys/values with
EnsureMapKeySequence(...)
andEnsureMapValueSequence(...)
- Control the number of items with
WithMinMapItems()
,WithMaxMapItems()
andWithExactMapItems()
- Set the keys/values with
The planned list of features is here