buffalo
buffalo copied to clipboard
Resource generator should generate default tests
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 i can take this one :)
@apaganobeleno if you need some help, just let me know.
@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.
@apaganobeleno yes of course. I would love to help out here.
@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
That makes sense.
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 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 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, 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 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, 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.
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
Will be looking into this once #1521 is merged, it should make this change a lot easier