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

Closed
EwenQuim opened this issue Jan 13, 2025 · 3 comments · Fixed by #351
Closed

Add a context usable in tests #349

EwenQuim opened this issue Jan 13, 2025 · 3 comments · 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
Contributor

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
Contributor

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
EwenQuim added a commit that referenced this issue Feb 1, 2025
* feat: add framework-agnostic mock context for testing

Implements a MockContext[B] type that allows testing controllers without
framework dependencies. Includes comprehensive tests and documentation.

* feat(test): add framework-agnostic mock context

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 #349

* feat(test): add realistic mock context example

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 #349

* Resolved the review comments

* Removed the TESTING.md file

* Documentation and testing updates

* Use internal.CommonContext to provide most functions in MockContext

* refactor(mock): make MockContext fields public and leverage CommonContext functionality

* feat(testing): Expose MockContext fields for simpler test setup

Convert private fields to public and update docs with examples

* fix(mock): Initialize OpenAPIParams in MockContext constructor

Properly initialize CommonContext fields to prevent potential panics

* Removed unused import

* feat(mock): Add helper methods for setting query params with OpenAPI validation

* Removed unused comment

* Linting

* Adds `NewMockContextNoBody` to create a new MockContext suitable for a request & controller with no body

Changed WithXxx to SetXxx for query parameters.
Removed the ...option pattern for declaring query parameters in the MockContext.

* Removed the available fields and methods section from the testing guide

It's not necessary to have the available fields and methods section in the testing guide, as it's already documented in the codebase & the godoc reference.

---------

Co-authored-by: EwenQuim <ewen.quimerch@gmail.com>
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