Created
October 14, 2016 12:22
-
-
Save tsmaeder/5b0937a4126936b21e016a4c025c0f02 to your computer and use it in GitHub Desktop.
Mock object pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type WorkItemRepository struct { | |
DoLoad func(ctx context.Context, ID string) (*app.WorkItem, error) | |
DoSave func(ctx context.Context, wi app.WorkItem) (*app.WorkItem, error) | |
DoDelete func(ctx context.Context, ID string) error | |
DoCreate func(ctx context.Context, typeID string, fields map[string]interface{}) (*app.WorkItem, error) | |
DoList func(ctx context.Context, criteria criteria.Expression, start *int, length *int) ([]*app.WorkItem, uint64, error) | |
} | |
func (r *WorkItemRepository) Load(ctx context.Context, ID string) (*app.WorkItem, error) { | |
return r.DoLoad(ctx, ID) | |
} | |
func (r *WorkItemRepository) Save(ctx context.Context, wi app.WorkItem) (*app.WorkItem, error) { | |
return r.DoSave(ctx, wi) | |
} | |
func (r *WorkItemRepository) Delete(ctx context.Context, ID string) error { | |
return r.DoDelete(ctx, ID) | |
} | |
func (r *WorkItemRepository) Create(ctx context.Context, typeID string, fields map[string]interface{}) (*app.WorkItem, error) { | |
return r.DoCreate(ctx, typeID, fields) | |
} | |
func (r *WorkItemRepository) List(ctx context.Context, criteria criteria.Expression, start *int, length *int) ([]*app.WorkItem, uint64, error) { | |
return r.DoList(ctx, criteria, start, length) | |
} | |
func testFunc() { | |
var repo models.WorkItemRepository = &WorkItemRepository{ | |
DoList: func(ctx context.Context, criteria criteria.Expression, start *int, length *int) ([]*app.WorkItem, uint64, error) { | |
return []*app.WorkItem{}, 0, nil | |
}, | |
} | |
ts:= ... | |
service:= ... | |
newcontroller := NewWorkitemController(service, repo, ts) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mock base implementation has a func field for each interface function. Member functions call the corresponding func field