Coder Social home page Coder Social logo

Comments (1)

kingeasternsun avatar kingeasternsun commented on June 30, 2024 1

I got it

package main

import (
	"fmt"

	"github.com/maraino/go-mock"
)

type Infost struct {
	A string
}

type Client interface {
	// Request(url *url.URL) (int, string, error)
	Info(a string) Infost
	Query() Infost
}

type MyClient struct {
	mock.Mock
}

// func (c *MyClient) Request(url *url.URL) (int, string, error) {
// 	ret := c.Called(url)
// 	return ret.Int(0), ret.String(1), ret.Error(2)
// }

func (c *MyClient) Info(a string) Infost {
	ret := c.Called(a)
	return ret.Get(0).(Infost)
}

func (c *MyClient) Query() Infost {
	ret := c.Called()
	return ret.Get(0).(Infost)
}

func main() {
	c := &MyClient{}

	// url, _ := url.Parse("http://www.example.org")
	// c.When("Request", url).Return(200, "{result:1}", nil)
	// // c.When("Request", url).Return(200, "{result:2}", nil)
	// c.When("Request", mock.Any).Return(500, "{result:0}", fmt.Errorf("Internal Server Error")).Times(1)

	// code, json, err := c.Request(url)
	// fmt.Printf("Code: %d, JSON: %s, Error: %v\n", code, json, err)

	// url, _ = url.Parse("http://www.github.com")
	// code, json, err = c.Request(url)
	// fmt.Printf("Code: %d, JSON: %s, Error: %v\n", code, json, err)

	c.When("Info", "a").Return(Infost{"love"})
	c.When("Query").Return(Infost{"lovequery"})
	info := c.Info("a")
	fmt.Println(info)

	queryInfo := c.Query()
	fmt.Println(queryInfo)

	if ok, err := c.Verify(); !ok {
		fmt.Println(err)
	}
}

from go-mock.

Related Issues (8)

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.