Dealing with Duplication

images/programming/duplication.png

Copying and pasting code is one of these double-edged swords in software. On the one hand, it’s great for quickly getting things up and running. But on the other hand, it makes our code more fragile and harder to change.

To see what I mean, take a look at the following test code. A common pattern in testing is to get the first test working, and then copy and paste the same code for the other test cases afterward, resulting in code that looks like this:

 test ​'can access welcome page'​ ​do
  @user = users(​:user1​)
  get login_path
  post login_path, ​session: ​{ ​email: ​​'user@test.com'​, ​password: ​​'password'​ } ...

Get The Way of the Web Tester now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.