maraino / go-mock

A mocking framework for the Go Programming Language.
MIT License
35 stars 12 forks source link

what if the function return a struct, how can i mock it? #14

Closed kingeasternsun closed 6 years ago

kingeasternsun commented 6 years ago
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() 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() Infost {
    ret := c.Called()
    return ret.GetType(0, Infost{}).(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", mock.Any).Return(Infost{"love"})
    info := c.Info()
    fmt.Println(info)

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

panic

panic: Mock call missing for Info()

goroutine 1 [running]:
github.com/maraino/go-mock.(*Mock).Called(0xc420074150, 0x0, 0x0, 0x0, 0x0)
        /gitlab/src/github.com/maraino/go-mock/mock.go:364 +0xf81
main.(*MyClient).Info(0xc420074150, 0xc420041f50, 0x1)
        /github/program-learn/Golang/src/maraino-go-mock/main.go:28 +0x4c
main.main()
        /github/program-learn/Golang/src/maraino-go-mock/main.go:48 +0x172
exit status 2
kingeasternsun commented 6 years ago

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)
    }
}