blog icon indicating copy to clipboard operation
blog copied to clipboard

Add "Guardian" test anti-pattern

Open danon opened this issue 6 years ago • 6 comments

Let me know If you approve another anti-pattern? :)

danon avatar May 22 '19 10:05 danon

@yegor256/z please, pay attention to this pull request

0crat avatar May 22 '19 10:05 0crat

@Danon this pull request is too small, just 4 lines changed (less than 10), there will be no formal code review, see §53 and §28; in the future, try to make sure your pull requests are not too small; @yegor256/z please review this and merge or reject

0crat avatar May 22 '19 10:05 0crat

Sorry. Still working out how this "review" stuff works... :)

johndotpage avatar May 22 '19 19:05 johndotpage

@Danon Have you got a clearer example of this? As far as asserting that something doesn't happen is concerned, it seems perfectly fine to assert that something doesn't throw, for example.

johndotpage avatar May 22 '19 19:05 johndotpage

What I mean is

// given
void shouldNotAppendExtensionForBlocked() {
  // given
  Resource r = new Resource("/content/docs/file.txt", "some word");
  r.makeWrittable(false);
  Creator c = new Creator();

  // when
  c.append("file.txt", "");

  // then
  assertEquals(r.get(), "some word");
}

How are you going to check if this test is not a Liar? Normally, you would comment the when section or remove logic from the Creator class, but in this case - anything you do won't make it fail.

Second question: how can you be sure your assertions are correct?

  • Maybe the class is valid and your code doesn't do what it wasn't supposed to do
  • Maybe the class is invalid and it does something what it wasn't supposed to, but assertions didn't pick it up

danon avatar May 22 '19 22:05 danon

@Danon I'm not sure I follow (maybe I've missed the point of the code). If the desired behaviour is that appending an empty string doesn't change the file at all, then that test has some value. Not a lot, sure. But some. (It's just an example after all, right?)

I would probably rewrite the test (and the code), but it would still be a specification of desired behaviour.

void appending_an_empty_string_to_a_text_file_does_not_change_the_files_contents() {
  Text file = new Text("/content/docs/file.txt", "some text");

  file.append("");

  assertEquals(file.contents(), "some text");
}

And now, if appending an empty string added "empty string was appended" or else threw an exception, then this test would catch it.

johndotpage avatar May 23 '19 19:05 johndotpage