title: How to properly test a controller class in Go
body: <p>I'm using gomock to generate a business layer and mock it's methods results. So far, I can't get the test to pass, it is saying that "want" and "got" values are different</p>
  
  <p>I'm passing a json representation of my object to strings.NewReader and the "Want" is coming with a value "is equal to { { ..." that might be the problem.</p>
  
  <pre><code>
  package product
  
  import (
      //...
  )
  
  var (
      productBody = `{"seller":{"id":"Foo"},"sku":"kj1293lkxpto","gtin":"7894949501280","name":"Foo","description":"Bar","legacyInfo":{"id":1021,"digit":4,"line":{"id":1,"family":{"id":3}},"situation":"EC"},"ncm":612522332,"origin":0,"unit":"kg","technicalDetails":{"volume":{"quantity":1},"weight":0.56,"height":100,"width":172.96,"length":100},"status":{"id": 1,"description":"active"}}`
          mockProduct  = model.Product{
          Seller:      model.Seller{ID: "Foo"},
          Sku:         "kj1293lkxpto",
          Gtin:        "7894949501280",
          Name:        "Foo",
          Description: "Bar",
          LegacyInfo: model.LegacyInfo{
              ID:    1021,
              Digit: 4,
              Line: model.Line{
                  ID: 1,
                  Family: model.Family{
                      ID: 3,
                  },
              },
              Situation: "EC",
          },
          Ncm:    612522332,
          Origin: 0,
          Unit:   "kg",
          TechnicalDetails: model.TechnicalDetails{
              Volume: model.Volume{
                  Quantity: 1,
              },
              Weight: 0.56,
              Height: 100,
              Width:  172.96,
              Length: 100,
          },
          Status: model.Status{
              ID:          1,
              Description: "active",
          },
      }
      e   *echo.Echo
      c   echo.Context
      req *http.Request
      rec *httptest.ResponseRecorder
  )
  
  func TestCreate(t *testing.T) {
      ctrl := gomock.NewController(t)
      defer ctrl.Finish()
  
      e = echo.New()
      e.Validator = middlewares.NewCustomValidator()
      rec = httptest.NewRecorder()
      req = httptest.NewRequest(http.MethodPost, "/v1/foo/bar", strings.NewReader(productBody))
      req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
      c = e.NewContext(req, rec)
  
      mockHandler := mock_product.NewMockIHandler(ctrl)
  
      mockHandler.EXPECT().Save(mockProduct).Return(&amp;mockProduct, nil) // line 37
  
      controller := NewController(mockHandler)
  
      controller.Create(c)
  }
  
  // -- controller.go
  package product
  
  import (
      //...
  )
  
  func NewController(handler IHandler) *Controller {
      return &amp;Controller{handler}
  }
  
  func (c *Controller) Create(ctx echo.Context) error {
  // ...
  }
  
  // -- handler.go
  // service
  package product
  
  type IHandler interface {
      Save(product *model.Product) (savedProduct *model.Product, err error)
  }
  
  func (h *Handler) Save(product *model.Product) (savedProduct *model.Product, err error) {
  //...
  }
  
  // -- product.go
  package model
  
  type Product struct {
      ID               string           `json:"id,omitempty"`
      Seller           Seller           `json:"seller,omitempty"  `
      Sku              string           `json:"sku,omitempty"  `
      Gtin             string           `json:"gtin,omitempty"`
      Name             string           `json:"name,omitempty"`
      Description      string           `json:"description,omitempty"`
      LegacyInfo       LegacyInfo       `json:"legacyInfo,omitempty"`
      Ncm              int64            `json:"ncm,omitempty"`
      Origin           int              `json:"origin,omitempty"`
      Unit             string           `json:"unit,omitempty"`
      TechnicalDetails TechnicalDetails `json:"technicalDetails,omitempty"`
      Status           Status           `json:"status,omitempty"`
      CreatedAt        Date             `json:"createdAt,omitempty"`
      UpdatedAt        Date             `json:"updatedAt,omitempty"`
  }```
  
  
  Expected call at /foo/bar/controller_test.go:37 doesn't match the argument at index 0.
          Got: &amp;{ {Foo} kj1293lkxpto 7894949501280 Foo Bar {1021 4 {1 {3}} EC} 612522332 0 kg {{1} 0.56 100 172.96 100} {1 active} {&lt;nil&gt;} {&lt;nil&gt;}}
          Want: is equal to { {Foo} kj1293lkxpto 7894949501280 Foo Bar {1021 4 {1 {3}} EC} 612522332 0 kg {{1} 0.56 100 172.96 100} {1 active} {&lt;nil&gt;} {&lt;nil&gt;}}
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>I did what @bigpigeon mentioned </p>
  
  <pre><code>    func TestCreate(t *testing.T) {
          ctrl := gomock.NewController(t)
          defer ctrl.Finish()
  
          b, err := json.Marshal(&amp;mockProduct)
          fmt.Println(err)
          fmt.Println(string(b))
  
          e = echo.New()
          e.Validator = middlewares.NewCustomValidator()
          rec = httptest.NewRecorder()
          req = httptest.NewRequest(http.MethodPost, "/v1/foo/bar", strings.NewReader(string(b)))
          req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
          c = e.NewContext(req, rec)
  
          mockHandler := mock_product.NewMockIHandler(ctrl)
  
          mockHandler.EXPECT().Save(&amp;mockProduct).Return(&amp;mockProduct, nil)
  
          controller := NewController(mockHandler)
  
          controller.Create(c)
      }
  
      &lt;nil&gt;
      {"id":"123","seller":{"id":"Foo"},"sku":"kj1293lkxpto","gtin":"7894949501280","name":"Foo","description":"Bar","legacyInfo":{"id":1021,"digit":4,"line":{"id":1,"family":{"id":3}},"situation":"EC"},"ncm":612522332,"unit":"kg","technicalDetails":{"volume":{"quantity":1},"weight":0.56,"height":100,"width":172.96,"length":100},"status":{"id":1,"description":"active"},"createdAt":"03-09-2019 00:41:56","updatedAt":"03-09-2019 00:41:56"}
      --- FAIL: TestCreate (0.00s)
          /home/ivo/Workspaces/GoNew/deckard-writer/api/routes/product/controller.go:41: Unexpected call to *mock_product.MockIHandler.Save([0xc0003aa0f0]) at /home/ivo/Workspaces/GoNew/deckard-writer/api/routes/product/mock/handler.go:39 because: 
              Expected call at /home/ivo/Workspaces/GoNew/deckard-writer/api/routes/product/controller_test.go:43 doesn't match the argument at index 0.
              Got: &amp;{123 {Foo} kj1293lkxpto 7894949501280 Foo Bar {1021 4 {1 {3}} EC} 612522332 0 kg {{1} 0.56 100 172.96 100} {1 active} {2019-09-03 00:41:56 +0000 UTC} {2019-09-03 00:41:56 +0000 UTC}}
              Want: is equal to &amp;{123 {Foo} kj1293lkxpto 7894949501280 Foo Bar {1021 4 {1 {3}} EC} 612522332 0 kg {{1} 0.56 100 172.96 100} {1 active} {2019-09-03 00:41:56.733635373 -0300 -03 m=+0.003845944} {2019-09-03 00:41:56.733635557 -0300 -03 m=+0.003846109}}
  </code></pre>
  
  <p>Same result</p>