So writing tests feels painful. What now?, by Stephanie Minn
Description
Writing tests for application code is often the first place a developer encounters friction. Stubbing endless methods to get to green. Fixing unrelated tests after a minor change. Rather than push on, let this tedium guide you towards writing code that’s easier to work with.
We’ll learn about coupling– the degree of dependence between software entities– to go from a troublesome test to an informed refactor. With examples in RSpec, this talk will serve as a practical guide towards reducing the pain of testing. Your future self will thank you!
Details
There’s a heuristic in software development telling us that when writing a test feels painful, it’s likely due to high coupling. We may want to alleviate that pain by reconsidering the design of our code. However, identifying and extracting areas of coupling can take years of experience to develop.
This talk is aimed at intermediate developers who are looking to level up in code quality and design but may feel unsure of where to begin. I’ll go over how to assess your own code, starting with a test suffering from symptoms of high coupling, and next steps for improving upon the first iteration of work.
Symptoms of coupling in my examples will include:
- Excessive factory setup
- Excessive stubbing or stubbing a chain of methods
- Asserting on application internals
- Complex use of RSpec
let
to obscure dependencies
Technical pillars I’ll cover include:
- Necessary coupling (cohesion)
- Unnecessary coupling
- Between test cases
- Between tests and application code
- Between test cases
- Connascence as a concept to specify types of coupling
An additional value proposition of this talk is teaching how to break down the process of refactoring into incremental steps. I’ll show how each iteration can be committed as a separate improvement, acknowledging the balance between time, effort, and quality in delivery. The technical concepts will be introduced as I walk through the following iterations towards looser coupling:
- Organizing an isolated a test to show its dependencies
- Organizing groups of tests in context blocks to highlight coupling
- Basic refactoring of the coupling identified by the previous iterations, such as extract method/class and dependency injection
- If I have time, writing tests for the extracted code to showcase the ease and improvement
Little or no familiarity with the concept of coupling in software is required. My expectation is for audience members to have experience writing tests in RSpec, including basic knowledge of mocking and stubbing. This talk is best suited for people who can already identify pain in writing tests and are curious about trying new strategies to alleviate their process. Additionally, this talk would be great for advanced Rubyists who are interested in coaching new developers in this skill.
Pitch
I enjoy giving the kinds of talks I wish I had seen earlier in my career. I care deeply about mentorship and supporting the day-to-day lived experience of other developers by fostering discovery rather than prescribing solutions.
Coupling is an oft-referenced and critical topic in developing great software, but not all engineers get a proper introduction to it. I hope to connect the dots between coupling and writing tests to provide them an opportunity to understand this concept more holistically.
I’m drawing on my extensive experience pairing with new developers to drive the content and value of this talk. Learning how to navigate coupling is often accelerated by working with someone more experienced. I recognize that not all developers have access to that kind of support, and ultimately I hope to provide an additional resource for folks to see what that process can look like by meeting them where they’re at.