jeudi 2 novembre 2017

What layer of abstraction should be tested

Let's say we have some functionality to write. It should validate csv file with some company data - one company per row. I'd like to write tests for this functionality. Today I had some conversation with my colleague and we can't decide which approach is better or maybe we are both wrong:

  1. Writing tests at the level of some service which is entry point of validation. It has one method:

    ProcessingResult process(CsvFile csvFile)
    
    

    The test for IBAN number looks like this:

    // given
    valid csv row with invalid value in IBAN column
    // when
    service called
    // then
    result is validation failure with INVALID_IBAN error reason
    
    
  2. Writing tests for specific validators. This approach assumes that there are few validators responsible for some groups of columns ex. CompanyNameValidator, AccountNumberValidator and so on.

First approach is good because it let us to refactor whole validation process without touching tests. I tried it and completely changed implementation running tests after every step and I was pretty confident I'm not breaking anything. This approach has cons as well. Let's say I have a test for company name and for IBAN. Consider implementation checking company name before IBAN. Let's say this name check is broken. Now test for IBAN is broken as well (the company name broke it). Is it ok or not?

Second approach won't have above problem as the company name is validated by different validator than IBAN. However it makes bigger refactoring impossible. If we wanted to change implementation and resign from multiple validators approach we would have to modify tests as well as we are testing them not the whole functionality.

Which approach is better or maybe there is another way to test this kind of code I'm not aware of?

Aucun commentaire:

Enregistrer un commentaire