buffalo icon indicating copy to clipboard operation
buffalo copied to clipboard

Resource generator should generate default tests

Open markbates opened this issue 7 years ago • 14 comments

Right now it generates tests that look like this:

func (as *ActionSuite) Test_BooksResource_List() {
	as.Fail("Not Implemented!")
}
...

It would be fantastic if these tests were actually implemented when generating a new resource.

This ticket is not for the faint of heart. :)

markbates avatar Apr 06 '17 15:04 markbates

@markbates i can take this one :)

paganotoni avatar Apr 06 '17 16:04 paganotoni

@apaganobeleno if you need some help, just let me know.

as27 avatar Apr 07 '17 08:04 as27

@as27 would you like to take this one? i'm afraid i wont have much time for the next month and don't want to delay this more.

paganotoni avatar Apr 26 '17 14:04 paganotoni

@apaganobeleno yes of course. I would love to help out here.

as27 avatar Apr 26 '17 19:04 as27

@markbates I would make the tests analog to the todo app. The tests will also use ActionSuite like here: https://github.com/gobuffalo/toodo/blob/master/actions/todos_test.go

as27 avatar Apr 28 '17 19:04 as27

That makes sense.

markbates avatar May 01 '17 16:05 markbates

The test generator is almost ready. But when I run the tests I receive always return code 500. I think I need a little help to understand why.

The template file for the generator would be here: https://github.com/as27/buffalo/blob/resource-test-generator/generators/resource/templates/actions/resource-use_model_test.go.tmpl

I made a own repository for a generated example. The output after code generation is: https://github.com/as27/buffaloresourcetest/blob/master/actions/users_test.go

But when I run buffalo test inside this repository I get always 500 as return code.

as27 avatar May 26 '17 08:05 as27

@as27 Giving a quick look, it seems you have a typo in https://github.com/as27/buffalo/blob/resource-test-generator/generators/resource/templates/actions/resource-use_model_test.go.tmpl line 49

{{.Singular}} -> {{.singular}}

The other errors seem linked to the lack of CSRF token in your request, but the suite utils should disable this middleware.

stanislas-m avatar May 28 '17 06:05 stanislas-m

@stanislas-m thanks. CSRF was the missing link I didn't see. But that issue is more a gobuffalo/suite issue then a generator thing (look here)? Or am I wrong?

as27 avatar May 28 '17 09:05 as27

@as27, as I understand the way the CSRF is handled in the test suite, it just replaces the middleware with a no-op. Maybe I'm wrong, but I think the app.Use gets a copy of the middleware, so, if you replace it after the app.Use call it can't work.

Maybe the app.Use should take a pointer to allow this kind of override?

edit: I found a Replace method in the middleware stack, maybe it can be used to fix the problem in gobuffalo/suite?

stanislas-m avatar May 28 '17 11:05 stanislas-m

@stanislas-m thank you. I had the same thoughts about this. A possible fix here would be to deactivate CSRF middleware inside the action_test.go file. But I think that this would be a bad idea. I think the tests also have to have valid requests inside the ActionSuite type. @markbates I would now make a PR just for the resource generator, because without the CSRF middleware the tests are passing. If you want I will open an issue for that at gobuffalo/suite.

as27 avatar May 28 '17 18:05 as27

@as27, I also think preventing the middleware to do its work is a bad thing. What we should test, is the normal case, with a given token.

The CSRF token is stored in the buffalo context, and in the session (key name is "authenticity_token"). Maybe you can do a first query to get a valid token, and manage to get the token from either the context or the session.

stanislas-m avatar May 28 '17 18:05 stanislas-m

Hello guys !

I really like the idea of having a basic set of tests generated automatically. Is it still something? Is someone working on this one?

Cheers

benjaminch avatar Oct 23 '18 07:10 benjaminch

Will be looking into this once #1521 is merged, it should make this change a lot easier

lukasschlueter avatar Jan 04 '19 18:01 lukasschlueter