Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking interfaces in main namespace #15

Closed
warmans opened this issue Apr 3, 2015 · 1 comment
Closed

Mocking interfaces in main namespace #15

warmans opened this issue Apr 3, 2015 · 1 comment

Comments

@warmans
Copy link
Contributor

warmans commented Apr 3, 2015

Hi,

I don't know if this is a stupid question or not but is it possible to generate mocks into a mocks "subpackage" when your interfaces are in main?

e.g.

main.go

package main

type Something struct {
}

type MyInterface interface {
     DoSomething() Something //return main.Something
}

func main() {
}

Will generate something like:

mocks/MyInterface.go

package mocks

import "github.com/stretchr/testify/mock"
import "github.com/warmans/example" //tries to import main

type MyInterface struct {
   mock.Mock
}

func (m *MyInterface) DoSomething() main.Something {
    //...
    return main.Something
}

which means the test looks like this:

main_test.go

package main

import (
    "testing"
    "github.com/warmans/example/mocks" //import the mocks subpackage
)

func TestDoSomethingDoesSomething() {
    mock = new(mocks.MyInterface) //create the mock
}

Couple of issues: there is import recursion which causes a import cycle not allowed in test error. Also Go seems to be a bit funny about importing and using main anyway.

I am able to work around the problem by supplying the inpkg=true flag but it seems cleaner to have the mocks subpackage.

Thanks

@evanphx
Copy link
Member

evanphx commented Apr 8, 2015

There is simply not way to handle this other than using inpkg, other than you don't put everything in the main namespace. So either just use inpkg or reorg the code to not use main (I only put the bare minimum in main).

@evanphx evanphx closed this as completed Apr 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants