Skip to content

Suggestion: Use string literals instead of named routes or URL helpers in tests #328

Open
@ohbarye

Description

@ohbarye

Hi. This is a proposal about named routes usage in tests. I'd like to hear what you think.

Proposal

Use string literals instead of named routes or URL helpers in tests.

# bad
get photos_path
get photo_path(id)
get edit_photo_path(id)
# good
get "/photos"
get "/photos/#{id}"
get "/photos/#{id}/edit"

and so on.

Reason

Integration test (e.g. request specs, system specs) is to mimic user eyes and actions. A user could see "/photos", but not "photos_path". I agree with the following idea.

The more your tests resemble the way your software is used, the more confidence they can give you.
https://twitter.com/kentcdodds/status/977018512689455106

From a programmer's view,

  • the bad pattern slightly increases cognitive load that forces me to build actual URLs in my mind.
  • the bad pattern sometimes discourages Test Driven Development.
    • In TDD, a test comes first. At this point, I know actual URL but I don't know the named routes because the routes.rb has not been edited yet. (=I don't care about named routes at that moment).

Possible Objection

named routes possibly have merits in some points.

  • You don't have to edit test cases when you edit routes.rb. It automatically follows up.
  • It could be consistent to use named routes in tests if you use named routes in production code.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions