Hello, and welcome to another episode of The Bike Shed, a weekly podcast from your friends at ThoughtBot about developing great software. I'm Stephanie Min. And I'm Joelle Kinville. And together, we're here to share a bit of what we've learned along the way. So, Joelle, what's new in your world? I had a really interesting conversation with one of our designers here at ThoughtBot about how to mock out screens for processes or a new bit of UI that you want to add.
And from a more design perspective, he tends to think of things in terms of user flows. It's sort of linear. User goes here, does that, does that, does that, and it tells a story. And then he asked me, if you were to get a Figma, how would you like it to be? And I thought a little bit about it. And the first thing that came to mind is I want to have error states. Like show me all the, not just the happy path through it, but all the ways that things can go wrong. And then bonus points, if you can show me how, like which steps in this path go to which errors and then which way back. So you, at this point, you can encounter an error that takes you to this different screen or it takes you to this, you know, you get a little error banner or something. But then how do you get out of that? Does that send you back to the beginning of the process? Does that send you to a previous step? Does that skip you forward to the end? Those sort of transitions are really helpful to know. And before you know it, what we've effectively created is a finite state machine.
But for just UI states, you've diagrammed a finite state machine in Figma with your designer. And that's kind of the perfect deliverable to me. Yeah, that's really cool. As you were saying that, I was thinking like, oh, what you really want is a flowchart. I bet Figma does that, right? I don't know. I've not worked with it enough, but it would be very cool if there was a view to see that kind of state machine flowchart. They've got to have something for that. And if they don't, it's boxes and arrows. So you can do that pretty easily yourself. But yeah, especially I think for when you're doing things like UI components, that becomes particularly important. An example that we talked through was sort of type ahead thing where it's a searchable, filterable dropdown where the results are populated by request to a server. You're not just filtering like a select box, but you're populating results in a...
I guess, drop down list based off of a search query that you're typing. And there's so many subtle edge cases you have to think about for that, such as because there's network latency, what happens when you start typing and there are no results that have populated yet or your results are stale? What happens when the network is slow or when there's a network error? How do you surface that in the UI? How do you surface something like getting no results back for your queries? You've typed something, no results come back. How do you differentiate that from a, we're waiting, no results have come in yet, but there will be some in the future, as opposed to, no, we did your thing, we found no results. Or things that straight up went wrong. And then if you start typing again, does that send you back to initial state? Does that send you to some intermediate state? There's a lot of complex edge cases you have to think about. And having that sort of finite state machine diagram really helps to understand.
all the different journeys that a user can have because I guess they're all together in one place. Yeah, I really like just the idea of a dev and a designer collaborating on that because there are almost certainly more error states than you might think when you're first kind of sketching out that user journey. And yeah, I'm sure that there's also just a lot of great discovery that can come out of that around what kinds of components can be shared and what kinds of error states are more similar than they are different. Yeah, I think definitely forcing you to think about what are the edge cases and flushing those out and then building the connections. Not just what are the possible error states, but thinking a little bit about how do you get into those error states. And then what's really important for the user journey, how do I get out of the error state? Yeah, yeah, that's a really great point. I think sometimes, at least I have certainly fallen into the trap of thinking like, oh, well, you've hit an error, then that's it, that's the end of the journey. But it's also not a very good user experience, right? We should be very clear about what happens next or like what the user should be able to do to resolve that for themselves. Like sometimes the answer is just give you a link that goes back to the homepage. Maybe that's the best that we can do. Likely the user's going to hit the back button on their browser. Does that?
Or are they going to try to refresh? There's some things that are a little bit out of your control. How do you handle those things? But definitely that sort of empathy with your user, like, okay, well, now they're stuck. Something went wrong. Maybe it's their fault. Maybe it's our fault. What do they do next? And I think that's a really interesting conversation to have with your product people, with your design people, because that's not often a conversation that's top of mind when you're designing an experience. Yeah. I think this could be an entire episode, actually, because I was going to say something a little bit salty about the answer is not tell them to go contact support. You know, what we really should do is probably bring a designer on as a guest and then just have a whole episode on dealing with error states, but from a design perspective. I'll bet that would be really eye-opening. Yeah.
I'm going to add that to the backlog right after we're done recording. You can probably have fun with it. Call it something like, you know, exceptions for designers or something like that. Stephanie, what is new in your world? I have a new tool to share that I've been really loving. It's kind of like solving all of my bookmark and like tabs and browser issues and just like all of my reading on the internet problems all in one. It's called Reader by Readwise, or I guess it was an app.
called Readwise, and then they built a new product called Reader. But it's a reading app, but it's very optimized for reading on the web. And I think this is very useful now, at least for me, because I read a lot of newsletters. I have been using, I think like RSS feeds are kind of having a bit of a comeback. And I was previously using Feedbin as an RSS reader. But this reader app also like integrates with just like other sources on the web, like YouTube videos or Twitter threads or whatever Twitter is now. And you can integrate all like the different places that you...
get content into this app. They have a really, really good, basically like read it later functionality. So if you don't have time for something right now, it's kind of, you can kind of treat it like an inbox to triage. So you can say like, oh, I'll leave this in my inbox because I'm going to get to it today or put it in a different category for later. And it has a really nice browser plugin that I've been using to basically send web articles straight to my inbox, my reading inbox.
So that is what has solved my, you know, just like keeping tabs open because I think one day I'll get to a problem. So I have been really enjoying this app. Have you used other sort of read it later apps? Or is this the first one that you've tried? This is the first one that I've tried. So maybe I just have been needing that. And this happens to be the first one that I've introduced into my life. But the other cool thing about it is that I had mentioned it was like originally just an app called Readwise. And what that was for was specifically for...
storing and resurfacing highlights. So as you're reading something in like an e-reader, for example, you can highlight a passage, but it's like kind of unclear what happens to that highlight. You maybe never revisit it ever again, but their original Readwise app would be kind of a spontaneous review of your past highlights to re-inspire you and to help you remember things that you noted in the past. And I think since they kind of are unifying their two products together, you get all that great reading functionality and some really good. highlighting functionality as well, where it has a pretty robust tagging and note system. And then I also get an email once a week on things that I've highlighted and things I've read before that is super useful to be like, oh, remember when I had this cool thought about something that I read? And maybe now I can make new connections with it and produce content or what have you. So yeah, I have been very pleasantly surprised by how much my life was lacking something like this. I like the idea of adding annotation and that you're not just consuming
content, but you're interacting with it and enriching it with your own thoughts. Yeah. Any big note takers out there, it also, I think, has integrations with Obsidian and Evernote, and it'll sync things that you've highlighted into your note taking platform. So it's kind of like a really positive feedback cycle to help you really start making connections and kind of have your own generative thoughts. On the topic of building things generatively, I think today we wanted to go into some more controversial practices around testing where you try to maybe dry them up or generate them various ways.
A sort of maybe basic way that I've seen this happen is generating test examples in a loop. So maybe you start with an array and you loop over each of those. And then for each item in the array, you have an it block. How do you feel about that practice? Is that something that you do sometimes? That's a very interesting transition. That's interesting. I have not done it in a while, but I certainly have in the past. And I'm trying to think if there was a good reason why I did that. You haven't done it in a while, but you have done it in a four. Whoa. Wow. Okay. Yeah. I mean, that is probably true.
Now that I'm thinking about it, I'm sure I felt like I needed to kind of check that everything in this array, verify that it looked correct. But in hindsight, I think that I wish I could have been just more explicit about it. And likely, I'm guessing that I probably didn't need as many items in that collection as I thought I did. And what I would have done now is maybe just have two or three repeated assertions for each of those items. Yeah. I don't know. What do you think about that? I tend to avoid that, partly because it forces every test to be absolutely identical.
And when they're not, it becomes really painful once you start. You have this test case that you're generating for each item in the array, but then you start having conditions where like, if the current item is this, add an extra assertion or something. And now it's an absolutely tangled mess. That's awful. Don't do that. Yeah, I like what you said about then the items have to be considered identical, but that may or may not actually be true. And by doing that, you're actually coupling those two items in your test. And that's the classic pitfall of like misapplying dry is you think two things are the same when they're actually just similar, and then they might want to diverge in the future and you've locked them together and allowing them to diverge then becomes really painful.
Another way that iterating over an array for tests is clunky is your error messages aren't quite as helpful in terms of understanding why something failed, because you can't just have a line number anymore. Because the line number goes to one test in your code, but maybe you're running 20 test examples as a result of that one line. So maybe you can interpolate some values into your docstring for the it block or something, if you're using something like RSpec, so that you know which of the items in the array the test failed for. But otherwise, it can get really tricky to find things out. And so it's a little bit more annoying to debug because you have that layer of indirection. Yeah, I was just thinking that I have definitely been in a position where I've tried to debug a test where the assertions are in a loop and then having to, even when you put like a debugger or something, you are hitting it every single time you iterate. And that is just very annoying to find the one kind of particular item that you're looking for. I think the like most extreme version of this that I've seen was somebody wrote their file as a single test case.
and then wrap that in a loop. But the loop wasn't just going over a flat array. It was this deeply nested structure of nested arrays of hashes of whatever. And your loop would then, based off of the keys and the current item and things like that, do things differently. So that they basically found a way to take a normal test file that just tests for a bunch of different things. Instead of encoding it as code, just encoded all of the way they wanted to test as a giant array of hashes or whatever, and then iterated over that and turned those into tests. It almost felt like writing an AST, an abstract syntax tree, describing the behavior you wanted, and then having a single...
almost eval function that generated tests. And that was really painful to extend. Wow. That sounds, I mean, I almost kind of feel for this person because maybe they were just really interested in like learning about that. And it was just the wrong application. Probably. It's probably a fun challenge to do on a side project or as a experimental thing, but maybe keep that particular idea out of your work projects. Well, can we talk a little bit about why people do this? Like why people start introducing abstractions or helper methods to deduplicate code in their tests? Yeah.
What would you say is the most common motivation for introducing any kind of abstraction into your tests? I suspect it's that, you know, you're just typing a lot of the same setup over and over again. And that gets a bit tiresome. Or maybe it's starting to feel like your tests are getting, each example is getting a bit long. And so you're like, oh, like I can just pull some of this duplicated setup out and then have to type fewer words for each test case. That would be my guess. I think for me, there's maybe three motivations. One is it's clunky to write because there's a lot, like the setup might be really heavy. Or the assertions, if I need...
to cover a bunch of things that are very similar. Now I'm writing 10 separate test cases or 10 separate tests that are all very similar to each other. And I'm like, oh, I could just throw this into a loop that'll make my life easier. So easier to write is a common motivation. The flip side of that is easier to read. If there's a lot of things going on in a test and I just look at it and like, what is this test even about? I might want some abstraction. And then finally, a lot of duplication just scratches that itch as a programmer that I have where I love abstractions and creating abstractions and engineering things even when they don't necessarily need to. And so sometimes you see a thing like, you know what? I should probably write an abstraction here where that's frankly maybe not necessary. So I think that would be a motivation where it's maybe not good justification. The other two though, make code easier to read or easier to write, I think are totally fine.
based off the tools you use to achieve those goals. I'm trying to think if I disagree with this or not. I mean, I guess in theory, I agree. But in practice, I would much rather see more duplication in tests than less. And I think I'm coming at this from the perspective of a test reader, where I kind of want to see everything. I'm almost certainly just reading a single test in isolation because I have kind of like maybe found the particular code that I want to change or maybe just like understand more about. And so I'm looking specifically at one test example. And maybe this also now kind of starts to get into like, where is that abstraction located? Like, is it?
in this test file, like hopefully. Otherwise, I would have to go search for it elsewhere if it's in like a share or special context or I don't know, something else. Even I think in a factory, things can get pulled into those and be turned into abstractions that are kind of obscured. But yeah, I want to do as little work as possible. And I do think you made a good point about like, if there's so much going on in the setup and you want to know what that means, maybe that is a cue that an abstraction might be helpful. But I almost wonder if the abstraction might be better applied to the application code rather than the test.
I mean, definitely you have to always think of if a test is painful to write, maybe it's a sign that the application code needs to be refactored. And I think that was a big point of your RailsConf talk. I think that we do use abstractions every day in our tests, even for really simple things. So you mentioned factories. Those are abstractions. You could write an active record create call for every record that you want. But we usually don't because active record objects have validations and that requires you to fill in every field that is required. Many or maybe even most of which are not relevant to the test.
So what factories give us is a way to say, create a user with the role admin and then fill in all the other attributes with I don't care, just as long as it passes validation because I need it to save to the database. But they are not relevant to this test. And so what's happened now is that I've written a test that has removed all of the extra noise that doesn't contribute to what I'm trying to do. That makes it easier for the reader without losing the parts that are important. The fact that this user is an admin.
Yeah, I do think that it gets a little bit tricky when your abstractions start also doing things that are not necessary for the test at all. I do think that a lot of test suites end up kind of bloated in that way. And you end up kind of seeing that in how long it takes for those tests to run. So maybe there's also something to be said about what level of abstraction is the test that you're writing and like how much setup needs to be done for a unit test versus more of an integration test, for example. And maybe you want to be a little bit pickier about your use of abstractions depending on that.
So it's interesting, you brought up the idea of abstractions doing more work than you need them to do. And I just talked about why I think factories are a great abstraction. In many cases, if you don't need to actually write to the database, a factory is a terrible abstraction. It's creating a bunch of records that you don't need that's slow. And if you don't need to persist to the database, then you don't need all of the extra data just to pass validations. If you just need an object in memory, then just call user.new with a role of admin. And it's not going to complain at you because it doesn't need that extra data because we don't need to pass validations for this particular test.
So that would be a situation where factories are the wrong abstraction. They don't solve the problem that you have, and they actually do extra work that you don't need to. So your tests are slower, and they've not added to the story you're trying to tell. Do you find that, kind of going back to the idea of levels of abstraction, do you find that there are certain types of tests that you tend to like to introduce abstractions more for? I like judiciously applied abstractions, I think, in all tests, because I will maybe use a factory in a unit test. I would say for system tests in particular, it can be really nice to write code at a higher level of abstraction so that when I'm reading it, I'm thinking very much in terms of what the user is doing. Oh, I go to this page. I click this link. I do these things. And sometimes the raw Capibara API, which is already an abstraction that we're introducing, is good enough. And sometimes I want something that's a little bit higher.
So maybe I'm using a page object. Maybe I'm just writing some helper methods. But giving a name to a thing that the user wants to do can be really helpful. And I think more importantly for readability, consistency. So that I can read and my test stays roughly at the same level of abstraction. So we have an admin. Admin signs in, goes to the dashboard, fills in a form, expects to see a thing. As opposed to dropping down to all of a sudden some nitty gritty details. And now you've got a mix of high level and low level mixed together. Yeah, I think I would agree when it comes to system tests. I was just thinking about...
Especially when you have kind of a, this actually is a great callback to what was new in your world around state machines and user journeys. I've run into an issue where I've had to write a system test that goes through every step that the user takes. Like they fill this out and then they click next and then they fill this next thing out. When really I was trying to test like the third step or like a variation of the third step, right? And sometimes having to be declarative about all those things that a user does is distracting. And so that is a nice situation where I could have a page object to kind of set it up in a way that's like, okay, like the user has already gotten to this point. So now let's test kind of the interaction from here.
So you mentioned the word distracting, and we've talked a little bit about the idea of having too much information or having not enough. And I think an idea that I have about tests is that they're a form of storytelling. And you think about what is masterful or compelling storytelling. Authors that have stripped away all of the extra that you don't need, that don't add to the story, and yet they give you just enough so that you actually understand what's going along. And I think this is the thing that particularly fantasy writers sometimes really struggle with. They've invested so much into the world building that they've just got to dump on you all the time and give you...
so many proper nouns and I can't keep track of it and they're not relevant to the chapter that I'm reading. That's so funny because I've been reading a lot of fantasy and you know when some of the really, really thick, intense trilogies or whatever that have like 700 pages each have the front matter of all the list of characters and I'm constantly referring back to that list. You're like, who is this person? That's a really great analogy in terms of you should not make your test reader go and have to find out who this person is just to understand what's going on in the current test. And so it's a little bit of a dance, right? Sometimes you can use abstraction to...
remove elements that are unnecessary. But if you use abstractions wrong, sometimes you remove too much. And now the reader of the test doesn't really understand why is this the expected behavior? I was just thinking about like, what's the perfect balance then? And I found, I mean, I think this is true of abstractions in general, but maybe is even more helpful in a test is like, I think the reader should be able to infer reasonably what is going on in the abstraction. I'm trying to think of like times when I've been surprised by something that has happened that either wasn't made explicit or was not the same as the method or helper method was named.
So this is interesting. You mentioned earlier that when you're reading a test, you don't want to have to jump through a bunch of helper methods or other abstractions. And ideally, you don't have to jump too far to get to what you need. what if the mark of a good abstraction is that it's in your test, but it's good enough that you don't need to go to definition? Yeah, I think I like that. Because I want to trust that it works for my test, even more so than in normal application code, I think. Because it's like, I really shouldn't need to change that behavior in a test. I think it should be abstract enough to, in some ways, take for granted. I'm thinking about auth being a pretty good use case for abstractions in tests, where it's like, yeah, we have this solid infrastructure that we know is not changing anytime soon. And a lot of things use it.
And I can generally reason about like, it says login as admin, great, I know what's going on there. And I don't have to go look it up or it likely is not doing anything else surprising. Yeah, well, you can, you trust that it works, but also just by reading it, you have a good sense of like what it does. And they're like, the implementation details are probably not going to matter too much. If you have a login as admin helper, that's probably fine. You know, you need to be logged in as admin for this feature test. Do you need to care exactly how this goes about doing that? Probably not. Ooh, I have another kind of potential heuristic for you too.
kind of get your feedback on. What do you think about, it's kind of an extension of what I was saying. It's like, I think good abstractions in your test shouldn't need to change all that frequently. Ooh, I think I would agree with that. If your abstractions have to change a lot or if they churn, that's probably a sign that you've not abstracted this quite right because you keep finding more edge cases where your abstraction didn't quite cover what you thought it was, what you thought it was doing. Or the things that you tried to abstract together and say, oh, this is a generic version of this thing actually aren't the same thing. Now they're trying to diverge and you're trying to like shove them together with more programming. And sometimes it's just let the two be separate. So yeah, I like that as a heuristic.
So we've talked a little about some heuristics for what we think would make good abstractions in tests. Abstractions where you don't need to go to definition and abstractions that don't tend to turn in their implementation. What are some concrete examples of abstractions that you like to use in tests or that you see in tests written by other people and you're like, oh, I like that? I have been really enjoying RSpecs just like fancier matchers. I used to not like them, or maybe I used to just not know about them or thought that they were too fussy. And I would have just written the code to do them, especially when it comes to matching like a structure in a hash or something like that. But I've been liking them a lot more. I think they give better error messages when things go wrong. And yeah, it conveys a little more intention, I think, especially when if I have to assert on like...
something in a response body or like a hash, I want to call attention to particular things, but maybe I don't care about some of the other things in the data. And I found this to be useful there as well. Yeah. One that I like is the array matcher. If I want to assert that an array contains certain elements, or maybe it has exactly these elements, but I don't really care about order. And I guess you could like do some kind of like sort both arrays and then do an equality comparison or something like that. But when you just want to say, hey, does this array contain these other elements? The array matcher I think is really nice for that.
And it gives you this nice little diff when things are wrong and you just get more specialized output than you would for the generic equals matcher. Yeah, exactly. And like you said, you could do it programmatically, but then you lose that intention that you want to convey, right? I think if I were to have seen code that sorts something and I'd be like, well, I'd be confused about why we need to do that. But maybe you were like trying to say like, oh, I want it to, I don't care about order, but that doesn't necessarily come across without that little extra meaning from the abstraction. How do you feel about writing custom matchers?
Is that a thing you've ever done? I don't think I've ever written a custom matcher, but I've used them. And I think the best ones are ones that I didn't realize were custom and thought they were just part of RSpec. Because they're just like, oh, this is so nice. I didn't realize RSpec gave me that. Oh, no, a colleague wrote that. Even better. Yeah. I think you've written some before, right? Occasionally. It's not a tool I reach for often, and maybe I should reach for it more frequently. I think it can be really nice for situations where you want some really specialized error messaging when you might be asserting on multiple things. So sometimes if you're asserting against some sort of custom...
output or something, and in order to make sure that everything's correct, you have to have three or four assertions at the end of your test that check different properties on an object you're returning or something like that. You can often bundle that up into a single assertion with one matcher that says, hey, give me this struct of output, and I expect this thing to have the value inside. One that I've seen, and I'm not sure if this is something that the library provides, if this is the thing that we wrote, but my current client project is using the dryRB suite of things. And dryRB has this concept of a result object. So a result is either a success or a failure. And that wraps some data, which is either some error message or the data that you were trying to get back successfully. You have to sort of, before you can get at the inner data, you have to check whether it's a success or a failure.
So what you could do is every time you're dealing with results in your tests, have an assertion where you're saying, expect thing to be success. Okay, now that we know there's success, expect thing.value to have this content. That's kind of clunky and you have to do it all the time and that the pair always go together. So we have a matcher that says expect results and the matcher is be success containing. And then it's a value. It's basically just equality, but it says build in that success checking for free. We always want to check for success before we check the inner value. So it's kind of a combo success plus equality matcher.
So if there are bundles of matchers that go together, that's a great use, I think, for a custom matcher. If you need better error messaging, or if you need to assert on just something where there's a lot of clunky processing that needs to happen. So that happened once where I was generating output files of a very particular format. And then you need to load the file into memory and parse out certain values from it and all that. And I just wrote a matcher that said, given this path or given this file.
I want to know that it's valid or something like that. And I could just point it to a thing and that the matcher took care of all the intricacies that didn't matter for the purpose of the test. Yeah, I like that a lot. And while you were saying that, I was just thinking about how I've seen some things that have been implemented as RSpec shared examples that I think would have been better as a custom matcher. And I don't know, I might even say that I would always prefer a custom matcher than a shared example. Ooh, that's interesting. I think it's because for an RSpec shared example, the syntax is saying like, it behaves like X and then...
In X, you've defined some behavior to do with that class or whatever. But in some ways, I almost think that a custom actor is just making those behaviors explicit. So rather than saying it's like this, it's just saying it, you mentioned you're using it for, you're describing a little bit more explicitly about the attributes of what it is rather than making the user go jump to that wherever a shared example is defined to find out what those behaviors are. I don't know. This is a very nascent thought that I'm having on the spot. I mean, going back to the heuristics that we set up earlier, I think that...
rarely do I need to jump to definition for a custom matcher. So for example, for the dryRB thing where I'm like testing out that a result is successful and has contained certain values. If I'm looking at the test above and I'm like, I'm copying parts of them like, oh, there's a B success containing matcher. I'm not going to jump to definition and be like, oh, I wonder what this does. I'm like, oh, that's so convenient. I like that. I'm going to use it in my code. It makes sense. Shared examples, I'm probably more likely than not to jump to definition and be like, oh, what does it mean to behave like a
whatever it ends up being. And maybe that's because they're overused or they're used in situations where they shouldn't be. But I find myself jumping to definition way more often with a shared example than I do with a custom matcher. Yeah. I do think that they couple more things together. That's true. And I've definitely run into issues where that coupling then is like, oh, shoot, I need to change something about the way that this assertion works. But I felt totally lost because I'm like, it's in a shared example. So what do I keep? What do I bring over? I don't know. I couldn't change the shared example because it was used in a bunch of other places. If anyone has experienced this, then you know. Whereas I think if it had been a custom matcher, you can just...
assert on something else, use a different matcher, right? It gives you a little more flexibility there. Yeah, maybe like custom matchers feel a little bit more like composition where you can pull in the pieces that you want. Whereas shared examples are more like inheritance where it's just like, oh, if you are this thing, now you're stuck with all these extra behaviors that maybe you didn't want. That's it. We've solved it. Just kidding. Are there ever situations where you've seen uses of shared examples that you actually like? Or has it generally been, oh, this is the wrong abstraction for the situation? I don't know. I would say not really. I think the other thing that bothers me about shared examples and shared context too is that they're just like one-liners that are really easy to miss.
At least for me, for some reason, I just, even if I'm like staring right at it, it does not compute in my head. Especially if they are at a weird level of nesting. Maybe it's at the top level, and then you have no idea if you're looking in the middle of a file until you get all that red failure messages when you run the test. So that's my anti-shared stuff in RSpec bias. I think I'm similar to you. I'm very suspicious of shared examples. Occasionally, I think they can be really nice if you're trying to enforce an interface or a set of semantics. So I used to be pretty hard against shared examples. And a couple years back, I was writing about...
value objects and how they're a great thing. But value objects for them to work properly, they need to follow a few semantic rules. So two instances that have the same value need to be equal to each other. And that's not the default in Ruby. If you just create two objects that wrap, you're creating a money object. And a $5 instance and a different $5 instance are not going to be the same because they're different objects in memory. So you need to compare by value, not by identity. And there's a few other rules similar to that. They have to hash similarly and things like that. And they're really easy to get wrong.
So if I'm on a project and somebody's writing a value object, I don't want to have to, like on every PR, be like, hey, did you think about hash quality correctly? I would really like to say we've encoded the semantics of what a correctly behaving value object are. in a set of shared examples. And then when you create your value object in the system, just include it, it behaves like a value object at the top, and it will tell you exactly all the like foot guns and edge cases that you forgot. Yeah, I like that a lot. And it kind of goes back to that here, so we're talking about where we've, those things should be coupled together because the bundle of those attributes is what makes a value object, a value object, I suppose. And kind of like what we were saying about them feeling like inheritance, you know, inheritance is not bad when used correctly, if you are kind of feeling very certain that your abstraction is a type of this, right? Exactly.
And it's the sort of thing where maybe it's not the first thing you should reach for. Maybe there are other abstractions you should reach for. But it is a tool that you have when you need something that's a little bit fancier. On the other end of the spectrum, an abstraction that I often hear people sort of recommend as a like almost abstraction light for those of us who are maybe less into abstractions is helper methods. Just define a method in your test file. How do you feel about those? Love them? Hate them? Indifference? I've done it. A ringing endorsement from Stephanie Min. Again, I think I do them more in more like integration level tests or higher level than that when I am setting up a handful of factories. Maybe they also even like relate to each other.
And then I've even done something fancy where I give them an argument to, maybe it's like I'm setting up a user, but it has a lot of particular things that need to also happen. But then I want to associate a particular post or something that maybe that's what I'm testing. And so the post can have different conditions. And then I will pass an argument into a setup user helper method. So all of those things are hooked up correctly. Yeah, I would say I found that useful when I know for sure that these are things that I need and that there is no variance in them. And then the thing that can vary, I make sure to inline in the test.
But sometimes just the setting of those associations can be distracting, like I've mentioned. And now that I'm thinking about it, I actually wish that I had been even more descriptive in my helper method names to be like, this is kind of the conditions of the setup I'm assuming in this test, or like the use case that I'm assuming for the code that I'm testing. Yeah, going back to what we said earlier, it's about storytelling and removing the extraneous. So I think sometimes, and you'll hear it from us as well, when we're pushing back on some of these extra abstractions, we'll say, oh, we like to have everything in the test. We want to see everything in the test.
Having everything in the test can be noisy. And sometimes that's just as hard to read as having not enough in the test. So there is a bit of a Goldilocks zone that you have to find. Yeah, I think you're making me think a lot about just the way that I've used it in the past. But in some ways, I would argue that everything is still in the test because it's clear where they came from. Maybe I would have that setup user return the user, set that as a variable in line in my test. And then maybe I would do something with it. But it's like, I always want things that are in the test to be clear where they came from. And so I think in that way, I like a helper method.
called from the test example, not like in a before block or anything like that, where if it does create something that is relevant, it will have been assigned to a variable. Those are my very particular ways of doing that. That is the thing that I like for helper methods. I do it in tests a lot, but I'll do it in controllers as well. is having helper methods return values rather than setting state. Yeah, that's a good rule of thumb. So in a controller, I tend to not have a set user method that will find a user and then assign it to an instance variable. Instead, I will have a find user helper method and then I'll say at user equals find user in the controller. Again, because I want to know what is actually happening. So we've talked a lot about abstractions and the way we do abstractions in tests and sort of what we like, what we don't like. An idea that I often hear floating around is that the rules for abstraction in tests are kind of different.
than the rules for abstracting in application code. Is that an idea that you subscribe to? And if so, what would you say are the differences between abstracting in tests versus in application code? Yeah, I would say that I think that is true. The differences for me are, like I said earlier about abstractions in tests shouldn't really change all too frequently, where I think that oftentimes the purpose of introducing abstractions in code is to support change. But I almost always veer on the side of more verbose tests and don't spend too much time worrying about or thinking about unifying things at a higher level until...
Yeah, I'd rather spend that time thinking about abstractions for my application code. What about you? Do you feel this way or no? I think I definitely have the same bias as you towards, if in doubt, doing fewer abstractions. And I think that's because tests that are too verbose cause less harm than tests that are overly abstracted. That being said, I might have a little bit of that bias in my application code as well. I think I tend to use similar heuristics for thinking about abstractions in application code as I do in test code. And particularly anytime I think of applying dry, don't repeat yourself. The core idea of dry is not to eliminate.
duplicated characters. It's to have a single source of truth. So if two things are actually the same thing, and that is repeated in multiple places, then when they all have to change together, you have to go and change it in five places. If they're all kind of similar, but not quite the same though, and they're in five places, maybe when things change, you only have to change three out of the five. That should not be abstracted. They're not the same thing. But if all five places always change in lockstep, then yeah, that's a great candidate for an abstraction. And that's true in application code as well.
I think where we're really bad in tests is we see a lot of things that assume that they're the same because they feel like they're the same. And maybe our heuristics about what is the same and what's not are off in tests. So we end up coupling a lot of things that really have no business being coupled together. Yeah, there's an article I really like. It's called Dry is About Knowledge. And that has helped me kind of frame it a little bit differently. It's even more so than things are changing together. But I think it's the reason it is impromptu to ask, like, why are they changing together? And nudges you even more to think about the reasons for change, not just that they happen to change. And just to go back to tests really quick, I really like what you said about verbose tests are generally more harmless, even more than verbose code, I think. And yeah, I think using abstractions to tell a good story in test.
makes a lot more sense to me than even an application code, because those tests are about specifying behavior. And I think that is more conducive to storytelling. That's just a little opinion I thought of. And in all of this, I think it's important to keep goals in mind. Why are you choosing to add or not add an abstraction? And oftentimes, the first thing that comes to mind is a sort of approximate reason. It's like, oh, because I want shorter tests or something like that, or I want to eliminate duplication. Great, but what value does that give? You're not just removing duplication just for the fun of it. So dig a little bit deeper. Why do you want to eliminate duplication?
And there are all sorts of valid reasons you might want to do that. We mentioned better readability and especially remembering that people who read are often coming in to read a single test, not the entire file. But also think about the sort of user path. And maybe this is a little bit like when we're in the intro when I was talking about a user gets into an error state and then how do they get out of it. A very common reason that people read your test is when it fails. What is that experience like?
And so then maybe you start thinking about, okay, well, I need better error messaging when this fails so that people understand why it failed. Maybe I need a custom matcher. Maybe I need some other context in the test. Or maybe they come in and it's like, there's so much setup and it's hundreds of lines and the person's just gonna be really overwhelmed. How do we make that experience better? Maybe pulling it out into a helper method is helpful. Maybe writing factories. But always keeping in mind that goal. Like, why am I doing this? Yeah, I really like what you said about thinking about what the user is doing.
Maybe this is a good place to wrap up, but I just had an idea of like, oh, you know what would be really fun, a fun game to help developers like better learn their code base is if there was just something that like would pull out a random test example and have you read it and see if you can understand what's going on. Maybe I'll develop that and that'll be my cool new product to help developers level up in understanding their code base. I think it'll almost be a fun sort of social thing that you do with your team, right? Pull up a random test and try to understand what it's doing and they get harder and harder. On that note, shall we wrap up? Let's wrap up.
Show notes for this episode can be found at bikeshed.thoughtbot.com. If you enjoyed listening, one really easy way to support the show is to leave us a quick rating or even a review in iTunes. It really helps other folks find the show. If you have any feedback or questions, you can reach us at hosts at bikeshed.fm via email. Thank you so much for listening to The Bike Shed, and we'll see you next week. Bye!