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

user_test.go Tests Fail | undefined: ModelSuite #38

Closed
zeknox opened this issue Mar 21, 2019 · 3 comments
Closed

user_test.go Tests Fail | undefined: ModelSuite #38

zeknox opened this issue Mar 21, 2019 · 3 comments

Comments

@zeknox
Copy link

zeknox commented Mar 21, 2019

Hi! I’ve installed the buffalo-auth plugin using the command go getgithub.com/gobuffalo/buffalo-auth. This completes properly and the buffalo-auth command runs properly from my $PATH.

I've run the buffalo-auth auth command against my project and this has generated proper auth structure; however, when running the default tests which are generated its throwing multiple errors:

$GOPATH/src/coke/models/user_test.go:7:11: undefined: ModelSuite
$GOPATH/src/coke/models/user_test.go:29:11: undefined: ModelSuite
$GOPATH/src/coke/models/user_test.go:48:11: undefined: ModelSuite

Looking at the default code which is generated, the test functions are passing in the argument (ms *ModelSuite) but this is not defined in a location that the test is aware of.

I've been able to resolve this issue by modifying the default code to include the ModelSuite struct.
Original Code generated by buffalo-auth:

package models_test

import (
	"coke/models"
)

func (ms *ModelSuite) Test_User_Create() {
	count, err := ms.DB.Count("users")
	ms.NoError(err)
	ms.Equal(0, count)

	u := &models.User{
		Email:                "mark@example.com",
		Password:             "password",
		PasswordConfirmation: "password",
	}
	ms.Zero(u.PasswordHash)

	verrs, err := u.Create(ms.DB)
	ms.NoError(err)
	ms.False(verrs.HasAny())
	ms.NotZero(u.PasswordHash)

	count, err = ms.DB.Count("users")
	ms.NoError(err)
	ms.Equal(1, count)
}

[... SNIP ...]

Fixed code which addresses the undefined condition and allows tests to run properly and pass. The primary additions are defining the struct and adding the additional import line.

package models_test

import (
	"coke/models"

	"github.com/gobuffalo/suite"
)

type ModelSuite struct {
	*suite.Model
}

func (ms *ModelSuite) Test_User_Create() {
	count, err := ms.DB.Count("users")
	ms.NoError(err)
	ms.Equal(0, count)

	u := &models.User{
		Email:                "mark@example.com",
		Password:             "password",
		PasswordConfirmation: "password",
	}
	ms.Zero(u.PasswordHash)

	verrs, err := u.Create(ms.DB)
	ms.NoError(err)
	ms.False(verrs.HasAny())
	ms.NotZero(u.PasswordHash)

	count, err = ms.DB.Count("users")
	ms.NoError(err)
	ms.Equal(1, count)
}

[... SNIP ...]

I'm curious if there is something I've done wrong in the way I've installed the plugin or generated the auth code. Is this something the buffalo-auth auth generator should add to the user_test.go file by default? Any insight on the proper work flow or testing process would be greatly appreciated.

@cmorbitzer
Copy link

I can confirm this issue exists when models_test.go, where ModelSuite is defined, belongs to the models package instead of the models_test package. This seems to be the default behavior for apps created with buffalo new. This is fixed by changing package models in models_test.go to package models_test.

@paganotoni
Copy link
Member

This one was covered in v1.0.8. please give it a shot and keep us posted.

@cmorbitzer
Copy link

@paganotoni That fixed it. Thanks!

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

3 participants