Question: Is it possible to reference testing.T in the tests? #9

Closed
adamstep opened this Issue Oct 12, 2013 · 10 comments

Comments

Projects
None yet
4 participants

Is it possible to reference testing.T in the tests? This is needed by gomock to create a new mock controller.

Owner

onsi commented Oct 12, 2013

All the tests run under one gotest so if gomock is doing funky global things with that testing.T you might run into test-pollution trouble.

In the bootstrap file you can do something like save off the testing.T to a global (ugh) and then pass that down to gomock.

As an aside I rarely use mocks and much prefer a dependency injection pattern and then home grown fakes of my injected dependencies. Tends to be more explicit and easier to control than mocks.

Onsi

On Oct 12, 2013, at 1:23 PM, Adam Stepinski notifications@github.com wrote:

Is it possible to reference testing.T in the tests? This is needed by gomock to create a new mock controller.


Reply to this email directly or view it on GitHub.

Owner

onsi commented Oct 12, 2013

Scratch that - just looked at gomock and it looks like you really need a new mock controller for each It. The mock controller is given a testing reporter that it uses to signal failure. Using testing.T would cause the whole suite to fail as soon as the first test fails.

Thankfully gomock actually takes an interface and it should be possible to write a simple wrapper around Ginkgo's global Fail that would satisfy that interface.

I can cook up an example if you'd like Adam but won't have time for a couple of days. Give it a shot though - shouldn't be too hard!

On Oct 12, 2013, at 1:23 PM, Adam Stepinski notifications@github.com wrote:

Is it possible to reference testing.T in the tests? This is needed by gomock to create a new mock controller.


Reply to this email directly or view it on GitHub.

Thanks, I'll try it out and let you know how it goes!

It works! The test suite continues to run even after the first test fails. Thanks again for the suggestion, we're planning to move all of our tests from gobdd to Ginkgo to take advantage of the extra features.

type GinkgoTestReporter struct {}

func (g GinkgoTestReporter) Errorf(format string, args ...interface{}) {
    Fail(fmt.Sprintf(format, args))
}

func (g GinkgoTestReporter) Fatalf(format string, args ...interface{}) {
    Fail(fmt.Sprintf(format, args))
}

var _ = Describe("Handler", func() {
    var (
        t GinkgoTestReporter
        mockCtrl *gomock.Controller
        mockDatabase *models.MockDatabase
        handler *Handler
    )

    BeforeEach(func() {
        mockCtrl = gomock.NewController(t)
        mockDatabaseModel = models.NewMockDatabase(mockCtrl)
        handler = NewHandler(mockDatabase)
    })

    AfterEach(func() {
        mockCtrl.Finish()
    })

    // It() calls here
})
Owner

onsi commented Oct 13, 2013

Great! I'll adapt your example and add a gomock-reporter sub package and some documentation.

As you use ginkgo if you come across any features you'd like or any pain points let me know and I'll be happy to help out.

On Oct 12, 2013, at 3:28 PM, Adam Stepinski notifications@github.com wrote:

It works! The test suite continues to run even after the first test fails. Thanks again for the suggestion, we're planning to move all of our tests from gobdd to Ginkgo to take advantage of the extra features.

type GinkgoTestReporter struct {}

func (g GinkgoTestReporter) Errorf(format string, args ...interface{}) {
Fail(fmt.Sprintf(format, args))
}

func (g GinkgoTestReporter) Fatalf(format string, args ...interface{}) {
Fail(fmt.Sprintf(format, args))
}

var _ = Describe("Handler", func() {
var (
t GinkgoTestReporter
mockCtrl *gomock.Controller
mockDatabase *models.MockDatabase
handler *Handler
)

BeforeEach(func() {
    mockCtrl = gomock.NewController(t)
    mockDatabaseModel = models.NewMockDatabase(mockCtrl)
    handler = NewHandler(mockDatabase)
})

AfterEach(func() {
    mockCtrl.Finish()
})

// It() calls here

})

Reply to this email directly or view it on GitHub.

brysgo commented Nov 17, 2013

In the mean time I dumped the above example into a package here.

Owner

onsi commented Nov 17, 2013

Thanks for the nudge brysgo :) I finally got around to this. I've implemented a simple Gomock test reporter under github.com/onsi/ginkgo/thirdparty/gomocktestreporter and updated the docs with usage details.

@brysgo I liked the fact that you added a NewController method -- seems convenient -- but I didn't do the same to avoid giving Ginkgo a direct dependency on gomock.

Any feedback appreciated, as always :)

@onsi onsi closed this Nov 17, 2013

brysgo commented Nov 17, 2013

Works great! Just added it to my project.

Owner

onsi commented Nov 17, 2013

Sweet. Thanks @brysgo

I saw this today, in case anyone doubts, it works and it works well. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment