• Stars
    star
    200
  • Rank 195,295 (Top 4 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created about 7 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go Interface Mocking Tool

Charlatan

Circle CI codecov.io BSD Go Report Card

Percolate's Go Interface Mocking Tool. Please read our introductory blog post.

Installation

go get github.com/percolate/charlatan

Usage

  charlatan [options] <interface> ...
  charlatan -h | --help

Options:

  -dir string
        input package directory [default: current package directory]
  -file value
        name of input file, may be repeated, ignored if -dir is present
  -output string
        output file path [default: ./charlatan.go]
  -package string
        output package name [default: "<current package>"]

If you would like the mock implementations to live in the same package as the interface definition then use the simplest invocation as a directive:

//go:generate charlatan Interface

or from the command line:

charlatan -file=path/to/file.go Interface

You can chose the output path using -output, which must include the name of the generated source file. Any intermediate directories in the path that don't exist will be created. The package used in the generated file's package directive can be set using -package.

Example

Given the following interface:

package example

//go:generate charlatan Service

type Service interface {
	Query(filter *QueryFilter) ([]*Thing, error)
	Fetch(id string) (*Thing, error)
}

Running go generate ... for the above package/file should produce the file charlatan.go:

package example

type QueryInvocation struct {
	Parameters struct {
		Filter *QueryFilter
	}
	Results struct {
		Ident1 []*Thing
		Ident2 error
	}
}

type FetchInvocation struct {
	Parameters struct {
		Id string
	}
	Results struct {
		Ident3 *Thing
		Ident4 error
	}
}

type FakeService struct {
	QueryHook func(*QueryFilter) ([]*Thing, error)
	FetchHook func(string) (*Thing, error)

	QueryCalls []*QueryInvocation
	FetchCalls []*FetchInvocation
}

func (f *FakeService) Query(filter *QueryFilter) (id1 []*Thing, id2 error) {
	invocation := new(QueryInvocation)
	invocation.Parameters.Filter = filter

	id1, id2 := f.QueryHook(filter)

	invocation.Results.Ident1 = id1
	invocation.Results.Ident2 = id2

	return
}

// other generated code elided ...

Now you can use this in your tests by injecting the FakeService implementation instead of the actual one. A FakeService can be used anywhere a Service interface is expected.

func TestUsingService(t *testing.T) {
	// expectedThings := ...
	// expectedCriteria := ...
	svc := &example.FakeService{
		QueryHook: func(filter *QueryFilter) ([]*Thing, error) {
			if filter.Criteria != expectedCriteria {
				t.Errorf("expected criteria value: %v, have: %v", filter.Criteria, expectedCriteria)
				return nil, errors.New("unexpected criteria")
			}
			return expectedThings, nil
		},
	}

	// use the `svc` instance in the code under test ...

	// assert state of FakeService ...
	svc.AssertQueryCalledOnce(t)
}

Create anonymous function implementations for only those interface methods that should be called in the code under test. This will force a panic if any unexpected calls are made to the mock implementation.

The generated code has godoc formatted comments explaining the use of the mock and its methods.

More Repositories

1

caffeine

Speed up your Android development
Java
414
star
2

mentions

Easily add @ mention functionality to your Android applications
Java
100
star
3

redset

A Redis-backed sorted set useful for coordinating distributed work.
Python
72
star
4

ec2-security-groups-dumper

Dump your EC2 Security Groups as a CSV or JSON file
Python
70
star
5

foam

A library to quickly and easily enable multiple monitoring & support platforms for your mobile apps
Java
64
star
6

jennifer

A node.js bridge between Github pull requests and Jenkins.
CoffeeScript
50
star
7

iamer

Dump and load your AWS IAM configuration into text files
Python
48
star
8

ddldump

Dump a clean version of the DDLs of your tables, so you can version them.
Python
29
star
9

pratt-parser

A Pratt Parser implementation in Python
Python
29
star
10

jsonmatch

`jsonmatch` is a small library for diffing Python JSON dictionaries in a flexible, informative way
Python
17
star
11

bfh

A Python DSL for schema transformations
Python
11
star
12

retry

Percolate's Go retry package
Shell
10
star
13

sdic

SQL Data Integrity Checker
Python
9
star
14

fvd

Font Variation Description for JavaScript
JavaScript
9
star
15

neue

A minimal web font loader
JavaScript
7
star
16

dynamodb-create-cloudwatch-alarms

Automate the creation of DynamoDB ProvisionedThroughput Read/Write Alarms
Python
7
star
17

rds-create-freestoragespace-alarms

Automate the creation of RDS FreeStoragepace Alarms
Python
5
star
18

wordpress

Plugin for managing custom template polling for Percolate -> WordPress
CSS
2
star
19

dripconfig

A tool for doing configuration nicely
Python
2
star
20

sashay

Generates API documentation from a RAML definition
JavaScript
2
star
21

rds-create-cpu-alarms

Automatically create CPU alarms on your RDS servers.
Python
1
star