Documentation
¶
Overview ¶
Package payloadfakes provides mock implementations of various Payload CMS services. These mocks can be used in unit tests to simulate the behavior of the Payload CMS API without making actual network requests.
## Services
The following mocks are provided:
- MockCollectionService: A mock implementation of the CollectionService interface.
- MockGlobalsService: A mock implementation of the GlobalsService interface.
- MockService: A mock implementation of the Service interface.
- MockMediaService: A mock implementation of the MediaService interface.
Each mock service allows you to define the behavior of its methods by setting the corresponding function fields. By default, the methods return an empty payloadcms.Response and a nil error.
## Usage
1. Create an instance of the mock service you need. 2. Define the behavior of the methods you are going to use by setting the corresponding function fields. 3. Use the mock service instance in your tests.
This allows you to fully control and assert the behavior of your code when interacting with the Payload CMS API.
## Example
func TestPayload(t *testing.T) {
// Create a new mock collection service
mockCollectionService := payloadfakes.NewMockCollectionService()
// Define the behavior of the FindByID method
mockCollectionService.FindByIDFunc = func(ctx context.Context,
collection payloadcms.Collection,
id int,
out any,
) (payloadcms.Response, error) {
// Custom logic for the mock implementation
return payloadcms.Response{}, nil
}
// Use the mock collection service in your tests
myFunctionUsingCollectionService(mockCollectionService)
}
Index ¶
- type MockCollectionService
- func (m *MockCollectionService) Create(ctx context.Context, collection payloadcms.Collection, in any, ...) (payloadcms.Response, error)
- func (m *MockCollectionService) DeleteByID(ctx context.Context, collection payloadcms.Collection, id any, ...) (payloadcms.Response, error)
- func (m *MockCollectionService) FindByID(ctx context.Context, collection payloadcms.Collection, id any, out any, ...) (payloadcms.Response, error)
- func (m *MockCollectionService) FindBySlug(ctx context.Context, collection payloadcms.Collection, slug string, out any, ...) (payloadcms.Response, error)
- func (m *MockCollectionService) List(ctx context.Context, collection payloadcms.Collection, ...) (payloadcms.Response, error)
- func (m *MockCollectionService) UpdateByID(ctx context.Context, collection payloadcms.Collection, id any, in any, ...) (payloadcms.Response, error)
- type MockGlobalsService
- type MockMediaService
- type MockService
- func (m *MockService) Delete(ctx context.Context, path string, v any) (payloadcms.Response, error)
- func (m *MockService) Do(ctx context.Context, method, path string, body any, v any) (payloadcms.Response, error)
- func (m *MockService) DoWithRequest(ctx context.Context, req *http.Request, v any) (payloadcms.Response, error)
- func (m *MockService) Get(ctx context.Context, path string, v any) (payloadcms.Response, error)
- func (m *MockService) Post(ctx context.Context, path string, in any) (payloadcms.Response, error)
- func (m *MockService) Put(ctx context.Context, path string, in any) (payloadcms.Response, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MockCollectionService ¶
type MockCollectionService struct {
FindByIDFunc func(ctx context.Context, collection payloadcms.Collection, id any, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
FindBySlugFunc func(ctx context.Context, collection payloadcms.Collection, slug string, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
ListFunc func(ctx context.Context, collection payloadcms.Collection, params payloadcms.ListParams, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
CreateFunc func(ctx context.Context, collection payloadcms.Collection, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
UpdateByIDFunc func(ctx context.Context, collection payloadcms.Collection, id any, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
DeleteByIDFunc func(ctx context.Context, collection payloadcms.Collection, id any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
}
MockCollectionService is a mock implementation of the CollectionService interface.
func NewMockCollectionService ¶
func NewMockCollectionService() *MockCollectionService
NewMockCollectionService creates a new fake collections stub.
func (*MockCollectionService) Create ¶
func (m *MockCollectionService) Create(ctx context.Context, collection payloadcms.Collection, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
Create calls the mock implementation.
func (*MockCollectionService) DeleteByID ¶
func (m *MockCollectionService) DeleteByID(ctx context.Context, collection payloadcms.Collection, id any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
DeleteByID calls the mock implementation.
func (*MockCollectionService) FindByID ¶
func (m *MockCollectionService) FindByID(ctx context.Context, collection payloadcms.Collection, id any, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
FindByID calls the mock implementation.
func (*MockCollectionService) FindBySlug ¶
func (m *MockCollectionService) FindBySlug(ctx context.Context, collection payloadcms.Collection, slug string, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
FindBySlug calls the mock implementation.
func (*MockCollectionService) List ¶
func (m *MockCollectionService) List(ctx context.Context, collection payloadcms.Collection, params payloadcms.ListParams, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
List calls the mock implementation.
func (*MockCollectionService) UpdateByID ¶
func (m *MockCollectionService) UpdateByID(ctx context.Context, collection payloadcms.Collection, id any, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
UpdateByID calls the mock implementation.
type MockGlobalsService ¶
type MockGlobalsService struct {
GetFunc func(ctx context.Context, global payloadcms.Global, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
UpdateFunc func(ctx context.Context, global payloadcms.Global, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
}
MockGlobalsService is a mock implementation of the GlobalsService interface.
func NewMockGlobalsService ¶
func NewMockGlobalsService() *MockGlobalsService
NewMockGlobalsService creates a new fake globals stub.
func (*MockGlobalsService) Get ¶
func (m *MockGlobalsService) Get(ctx context.Context, global payloadcms.Global, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
Get calls the mock implementation.
func (*MockGlobalsService) Update ¶
func (m *MockGlobalsService) Update(ctx context.Context, global payloadcms.Global, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
Update calls the mock implementation.
type MockMediaService ¶
type MockMediaService struct {
UploadFunc func(ctx context.Context, f *os.File, in, out any, opts payloadcms.MediaOptions) (payloadcms.Response, error)
UploadFromURLFunc func(ctx context.Context, url string, in, out any, opts payloadcms.MediaOptions) (payloadcms.Response, error)
}
MockMediaService is a mock implementation of the MediaService interface.
func NewMockMediaService ¶
func NewMockMediaService() *MockMediaService
NewMockMediaService creates a new fake media service stub.
func (*MockMediaService) Upload ¶
func (m *MockMediaService) Upload(ctx context.Context, f *os.File, in, out any, opts payloadcms.MediaOptions) (payloadcms.Response, error)
Upload calls the mock implementation.
func (*MockMediaService) UploadFromURL ¶
func (m *MockMediaService) UploadFromURL(ctx context.Context, url string, in, out any, opts payloadcms.MediaOptions) (payloadcms.Response, error)
UploadFromURL calls the mock implementation.
type MockService ¶
type MockService struct {
DoFunc func(ctx context.Context, method, path string, body any, v any) (payloadcms.Response, error)
DoWithRequestFunc func(ctx context.Context, req *http.Request, v any) (payloadcms.Response, error)
GetFunc func(ctx context.Context, path string, v any) (payloadcms.Response, error)
PostFunc func(ctx context.Context, path string, in any) (payloadcms.Response, error)
PutFunc func(ctx context.Context, path string, in any) (payloadcms.Response, error)
DeleteFunc func(ctx context.Context, path string, v any) (payloadcms.Response, error)
}
MockService is a mock implementation of the Service interface.
func NewMockService ¶
func NewMockService() *MockService
NewMockService creates a new fake service stub.
func (*MockService) Delete ¶
func (m *MockService) Delete(ctx context.Context, path string, v any) (payloadcms.Response, error)
Delete calls the mock implementation.
func (*MockService) Do ¶
func (m *MockService) Do(ctx context.Context, method, path string, body any, v any) (payloadcms.Response, error)
Do calls the mock implementation.
func (*MockService) DoWithRequest ¶
func (m *MockService) DoWithRequest(ctx context.Context, req *http.Request, v any) (payloadcms.Response, error)
DoWithRequest calls the mock implementation.
func (*MockService) Get ¶
func (m *MockService) Get(ctx context.Context, path string, v any) (payloadcms.Response, error)
Get calls the mock implementation.
func (*MockService) Post ¶
func (m *MockService) Post(ctx context.Context, path string, in any) (payloadcms.Response, error)
Post calls the mock implementation.
func (*MockService) Put ¶
func (m *MockService) Put(ctx context.Context, path string, in any) (payloadcms.Response, error)
Put calls the mock implementation.