Skip to content

Refactor: class-based view for Core Index - #3063

Merged
angela-tran merged 1 commit into
mainfrom
refactor/cbv-core-index
Aug 6, 2025
Merged

Refactor: class-based view for Core Index#3063
angela-tran merged 1 commit into
mainfrom
refactor/cbv-core-index

Conversation

@angela-tran

Copy link
Copy Markdown
Member

Closes #2996

Reviewing

Index page loads as normal, and session is reset.

@angela-tran angela-tran self-assigned this Aug 5, 2025
@angela-tran
angela-tran requested a review from a team as a code owner August 5, 2025 22:45
@github-actions github-actions Bot added tests Related to automated testing (unit, UI, integration, etc.) back-end Django views, sessions, middleware, models, migrations etc. and removed tests Related to automated testing (unit, UI, integration, etc.) labels Aug 5, 2025
@github-actions

github-actions Bot commented Aug 5, 2025

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  benefits/core
  urls.py
  views.py
Project Total  

This report was generated by python-coverage-comment-action

@thekaveman thekaveman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, works as expected 👍

assert view.template_name == "core/index.html"

def test_get(self, view, app_request, mocked_session_reset):
response = view.get(app_request)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems perfectly fine for this test case, but as a note...

We found in other cases, like where you have view mixins, it is more appropriate (necessary even) to call view.dispatch() in a test, and then assert that your intended method was called (or not, depending on e.g. the mixin behavior). This is also more similar to how the framework calls into CBVs too.

Here's an example: https://github.com/cal-itp/benefits/blob/main/tests/pytest/eligibility/test_views.py#L355

In that case, calling view.post() directly would completely skip the *SessionRequiredMixins and thus e.g. self.flow in the view would not be set.

@angela-tran angela-tran Aug 6, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I noticed that we were doing that with view.dispatch() in tests for other CBVs. I think in a dev workshop discussion, we said the test should call the actual method we're implementing so that it is as close to a unit test as possible. It makes sense why the mixin doesn't get called if we do that though (the whole MRO thing).

What do you think about the approach taken in https://github.com/cal-itp/benefits/blob/main/tests/pytest/enrollment_switchio/test_views.py#L39-L40, where the variables that would be set by the mixins are set up in the fixture? I'm fine with doing it whichever way you think is best.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll go ahead and merge this PR since the issue doesn't apply here! Thanks for noting this so that we can get alignment on it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in a dev workshop discussion, we said the test should call the actual method we're implementing so that it is as close to a unit test as possible.

I recall this conversation as well, and yeah I realize what I just said above goes the other way...

What do you think about the approach taken in...

This could work too. I guess when I was working on the Eligibility CBVs, I was loathe to mock more things and just wanted to run my damn logic 😅 We can discuss in another workshop!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just putting this follow-up here for posterity.

We've spoken again about this in some various calls, and have implemented a few more CBVs, and this approach seems better overall. That is:

  • Initialize the CBV attributes like agency or flow as part of fixture setup
  • Test the actual CBV function: get(), post(), etc.

@angela-tran
angela-tran merged commit 46c0962 into main Aug 6, 2025
17 checks passed
@angela-tran
angela-tran deleted the refactor/cbv-core-index branch August 6, 2025 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

back-end Django views, sessions, middleware, models, migrations etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Class-based views: Core index

2 participants