1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
// +build ignore
// Simple application to show the use of go-mock.
package main
import (
"fmt"
"net/url"
"github.com/maraino/go-mock"
)
type Client interface {
Request(url *url.URL) (int, string, error)
}
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 main() {
c := &MyClient{}
url, _ := url.Parse("http://www.example.org")
c.When("Request", url).Return(200, "{result:1}", nil).Times(1)
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)
if ok, err := c.Verify(); !ok {
fmt.Println(err)
}
}
|