Search
⌘K
    to navigateEnterto select Escto close

    Have meaningful name in tests

    Developers often take liberties when creating data in test files. It's true, the data doesn't matter.

    Well, it doesn't matter to machines, but it does to humans.

    Just because it's a test program doesn't mean that you should create nonsensical names. Having meaningful names makes it easier to understand the business logic.

    Here's a snippet of code from a test file:

    1EmailLog.create_from_payload( to: [ 'qwe1@example.com',
    2                                    'qwe2@example.com'],
    3                              from: ['support@example.com'])
    4
    

    I am not sure what qwe1 and qwe2 are supposed to mean here. It would be much better if the names were like the ones given below:

    1EmailLog.create_from_payload( to: [ 'mike@example.com',
    2                                    'mary@example.com'],
    3                              from: ['support@example.com'])
    4
    

    Another example of a meaningful name

    1def test_email_is_normalized
    2  user = User.create! email: ' strAngeEmail@example.com'
    3  assert_equal 'strangeemail@example.com', user.email
    4end

    In the above case, it is not clear why the email is strange. A better name would help.

    1def test_email_is_normalized
    2  user = User.create! email: ' thisIsAMixedCaseEmail@example.com'
    3  assert_equal 'thisisamixedcaseemail@example.com', user.email
    4end
    Previous
    Next