When you’re working with a bare-bones testing framework like ExUnit, you’ll experience significant repetition when you set up code. Consider this testing script:
| ... |
| |
| setup context do |
| set_up_web_app |
| end |
| |
| test "index, logged in" do |
| user = new_user context |
| log_in user |
| |
| response = get("/") |
| assert response.code == 200 |
| assert response.template == "index" |
| end |
| |
| test "profile, logged in" do |
| user = new_user context |
| log_in user |
| |
| response = get("/#{user.id}") |
| assert response.code == 200 |
| assert response.template == "profile" |
| end |
| |
| test "profile, logged out" do |
| response = get("/") |
| assert response.code == 404 |
| end |
This hypothetical code runs some simple tests and correctly uses the setup macro to control the duplication of the code to set up the web application. Still, you can see more duplication. Every test that has to log in replicates code to log in and create a user. The problem is that in real-world testing situations, you’ll always have significant setup code, and this code causes a lot of duplication.