Giter Club home page Giter Club logo

mocketcdclient's Introduction

mockEtcdClient

Mock library to simulate interactions with etcd as if by the github.com/coreos/etcd/client library.

Starting with a faked KeysAPI, tests look like this (from mock_test.go):

package mockEtcdClient

import (
	"errors"
	"fmt"
	"testing"

	"github.com/coreos/etcd/client"
	. "github.com/onsi/gomega"
	"golang.org/x/net/context"
)

func TestExpectGet(t *testing.T) {
	RegisterTestingT(t)
	var kapi client.KeysAPI
	mock := FakeKeysAPI{}
	kapi = &mock
	mock.ExpectGet("/test/key").WillReturnValue("test value")

	resp, err := kapi.Get(context.Background(), "/test/key", nil)

	Expect(err).NotTo(HaveOccurred())
	Expect(resp).NotTo(BeNil())
	Expect(resp.Node).NotTo(BeNil())
	Expect(resp.Node.Key).To(Equal("/test/key"))
	Expect(resp.Node.Value).To(Equal("test value"))
	err = mock.ExpectationsFulfilled()
	Expect(err).NotTo(HaveOccurred())
}

func TestExpectErr(t *testing.T) {
	RegisterTestingT(t)
	var kapi client.KeysAPI
	mock := FakeKeysAPI{}
	kapi = &mock
	key := "/test/key"
	mock.ExpectGet(key).WillReturnError(errors.New(fmt.Sprintf("100: Key not found (%v) [39881395]", key)))

	resp, err := kapi.Get(context.Background(), "/test/key", nil)
	Expect(err).To(HaveOccurred())
	Expect(err.Error()).To(Equal(fmt.Sprintf("100: Key not found (%v) [39881395]", key)))
	Expect(resp).To(BeNil())
}

func TestExpectSet(t *testing.T) {
	RegisterTestingT(t)
	var kapi client.KeysAPI
	mock := FakeKeysAPI{}
	kapi = &mock

	mock.ExpectSet("/some/key", "some value")
	resp, err := kapi.Set(context.Background(), "/some/key", "some value", nil)
	Expect(err).NotTo(HaveOccurred())
	Expect(resp.Node).NotTo(BeNil())
	Expect(resp.Node.Key).To(Equal("/some/key"))
	Expect(resp.Node.Value).To(Equal("some value"))
	err = mock.ExpectationsFulfilled()
	Expect(err).NotTo(HaveOccurred())
}

func TestExpectSetError(t *testing.T) {
	RegisterTestingT(t)
	var kapi client.KeysAPI
	mock := FakeKeysAPI{}
	kapi = &mock

	mock.ExpectSet("/some/key", "some value").WillReturnError(errors.New("this is a testing error"))
	resp, err := kapi.Set(context.Background(), "/some/other/key", "some value", nil)
	Expect(err).To(HaveOccurred())
	Expect(resp).To(BeNil())
	err = mock.ExpectationsFulfilled()
	Expect(err).NotTo(HaveOccurred())
}

func TestExpectSetNotRecvd(t *testing.T) {
	RegisterTestingT(t)
	mock := FakeKeysAPI{}

	mock.ExpectSet("/some/key", "some value")
	err := mock.ExpectationsFulfilled()
	Expect(err).To(HaveOccurred())
}

mocketcdclient's People

Contributors

danieltmiles avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.