franela / goblin

Minimal and Beautiful Go testing framework
MIT License
887 stars 79 forks source link

Add support for mocking library #68

Closed piclemx closed 6 years ago

piclemx commented 6 years ago

Will be cool if we can have the possibility to make it work with goblin gomock, mockery or others.

marcosnils commented 6 years ago

gomock and other mocking libraries usually rely. on *testing.T to work. As you can access that from goblin I believe it should work without too many effort. Have you tried to setup an example?

piclemx commented 6 years ago

@marcosnils you can access it but when the mock fail it's not goblin error handler.

marcosnils commented 6 years ago

@piclemx do you have a simple example where this fails?.

artem-malko commented 4 years ago

@marcosnils there is an example, based on official example in testify:

package yours

import (
  "testing"
  "github.com/stretchr/testify/mock"
)

type MyMockedObject struct{
  mock.Mock
}

func (m *MyMockedObject) DoSomething(number int) (bool, error) {
  args := m.Called(number)
  return args.Bool(0), args.Error(1)
}

/*
  Actual test functions
*/

// TestSomething is an example of how to use our test object to
// make assertions about some target code we are testing.
// make assertions about func result
func TestSomething(t *testing.T) {
  g := Goblin(t)

  g.Describe("Test", func() {
    g.It("Test", func() {    
      testObj := new(MyMockedObject)
      testObj.On("DoSomething", 123).Return(true, nil)
      res := targetFuncThatDoesSomethingWithObj(testObj)

      // So, if this failed, we will see "1 tests failed" in output
      g.Assert(res).Equal(nil)

      // assert that the expectations were met
      // But if this failed — "1 tests complete"
      // Exit code will be 1, it is ok. And there will be a lot of logs about unexpected call,
      // but every thing will be ok in goblin output
      testObj.AssertExpectations(t)
  })
  })
}
artem-malko commented 4 years ago

@marcosnils as I can see, https://github.com/franela/goblin/pull/55 will fix problem from that issue =) Let's finish https://github.com/franela/goblin/pull/55?

artem-malko commented 4 years ago

@marcosnils never mind, different problem