All Projects → percolate → Charlatan

percolate / Charlatan

Licence: bsd-3-clause
Go Interface Mocking Tool

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Charlatan

DeepfakeHTTP
DeepfakeHTTP is a web server that uses HTTP dumps as a source for responses.
Stars: ✭ 373 (+91.28%)
Mutual labels:  mock, testing-tools
Mongodb Memory Server
Spinning up mongod in memory for fast tests. If you run tests in parallel this lib helps to spin up dedicated mongodb servers for every test file in MacOS, *nix, Windows or CI environments (in most cases with zero-config).
Stars: ✭ 1,376 (+605.64%)
Mutual labels:  testing-tools, mock
Miragejs
A client-side server to build, test and share your JavaScript app
Stars: ✭ 4,384 (+2148.21%)
Mutual labels:  mock, testing-tools
deckard
DNS test harness
Stars: ✭ 28 (-85.64%)
Mutual labels:  mock, testing-tools
Superagent Mocker
Pretty simple in-browser mocks for CRUD and REST API
Stars: ✭ 127 (-34.87%)
Mutual labels:  testing-tools, mock
mockafka
A testing DSL for kafka-streams
Stars: ✭ 14 (-92.82%)
Mutual labels:  mock, testing-tools
Webmockr
R library for stubbing and setting expectations on HTTP requests
Stars: ✭ 37 (-81.03%)
Mutual labels:  testing-tools, mock
main
Mocks Server monorepo
Stars: ✭ 109 (-44.1%)
Mutual labels:  mock, testing-tools
Ava Playback
📼 🚀 Record and playback http requests from your ava tests
Stars: ✭ 124 (-36.41%)
Mutual labels:  testing-tools, mock
Nsubstitute
A friendly substitute for .NET mocking libraries.
Stars: ✭ 1,646 (+744.1%)
Mutual labels:  testing-tools, mock
dubbo-mock
dubbo mock web server
Stars: ✭ 62 (-68.21%)
Mutual labels:  mock, testing-tools
Mockito
Most popular Mocking framework for unit tests written in Java
Stars: ✭ 12,453 (+6286.15%)
Mutual labels:  testing-tools, mock
eaf-linter
🤪 A linter, prettier, and test suite that does everything as-simple-as-possible.
Stars: ✭ 17 (-91.28%)
Mutual labels:  testing-tools, code-generation
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (-75.38%)
Mutual labels:  mock, testing-tools
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (-49.23%)
Mutual labels:  mock, testing-tools
Lyrebird
移动应用插件化测试工作台
Stars: ✭ 663 (+240%)
Mutual labels:  testing-tools, mock
mocat
🐈 Mocat is a mocking toolbar that allows you to interactively develop and test network requests.
Stars: ✭ 27 (-86.15%)
Mutual labels:  mock, testing-tools
ts-mock-imports
Intuitive mocking library for Typescript class imports
Stars: ✭ 103 (-47.18%)
Mutual labels:  mock, testing-tools
Prig
Prig is a lightweight framework for test indirections in .NET Framework.
Stars: ✭ 106 (-45.64%)
Mutual labels:  testing-tools, mock
Httpretty
Intercept HTTP requests at the Python socket level. Fakes the whole socket module
Stars: ✭ 1,930 (+889.74%)
Mutual labels:  testing-tools, mock

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].