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

Add a context usable in tests #349

Open
EwenQuim opened this issue Jan 13, 2025 · 3 comments · May be fixed by #351
Open

Add a context usable in tests #349

EwenQuim opened this issue Jan 13, 2025 · 3 comments · May be fixed by #351
Assignees
Labels
good first issue Good for newcomers

Comments

@EwenQuim
Copy link
Member

Is your feature request related to a problem? Please describe.

Each framework (net/http, gin...) have a private context struct that respects the ContextWithBody[B] interface.

But when testing your controller that have the func (c ContextWithBody[B]) (T, error) signature, we need a struct that respect this interface. Currently, users have to provide their struct, like in examples/gin-compat/main_test.go.

Describe the solution you'd like

That we provide a ContextMock[B] with a url.Values and a Body B that the user can simply fill in its tests, like in extra/fuegogin/context_mock.go but router-agnostic.

For the user, that means that he's testing his logic and not the framework: he trusts our serialization, validation etc...

That makes users more Fuego-dependant, which is not a good thing, but it's also more convenient than to use httptest and rewrite all utils. This also feels weird if the user have controllers that they cannot directly test.

@EwenQuim EwenQuim added the good first issue Good for newcomers label Jan 13, 2025
@olisaagbafor
Copy link

olisaagbafor commented Jan 13, 2025

@EwenQuim Mind if I take this issue?

@EwenQuim
Copy link
Member Author

If you want it, it's yours!

As usual (not fun but very helpful), don't forget tests and documentation. Happy coding! 🔥

@EwenQuim EwenQuim assigned EwenQuim and olisaagbafor and unassigned EwenQuim Jan 13, 2025
@olisaagbafor
Copy link

Thanks @EwenQuim 😊

olisaagbafor added a commit to olisaagbafor/fuego that referenced this issue Jan 13, 2025
Add MockContext[B] to help users test their controllers without framework
dependencies. Includes realistic examples and documentation.

- Move testing docs from README.md to TESTING.md
- Add mock context with getters/setters
- Add example user controller tests
- Use fuego_test package for end-user perspective

Closes go-fuego#349
olisaagbafor added a commit to olisaagbafor/fuego that referenced this issue Jan 13, 2025
Replace basic getter/setter tests with a real-world example:
- Add UserController with Create and GetByID endpoints
- Add UserService with mock implementation
- Show proper separation of concerns
- Demonstrate practical testing patterns
- Move testing docs from README.md to TESTING.md

This provides users with a clear example of how to test their
controllers using the mock context in a real-world scenario.

Closes go-fuego#349
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants