Mastering API Testing: Using Shoulda Matchers and Super Diff in Ruby - RUBY 640 - podcast episode cover

Mastering API Testing: Using Shoulda Matchers and Super Diff in Ruby - RUBY 640

Jun 12, 202432 min
--:--
--:--
Download Metacast podcast app
Listen to this episode in Metacast mobile app
Don't just listen to podcasts. Learn from them with transcripts, summaries, and chapters for every episode. Skim, search, and bookmark insights. Learn more

Episode description

Elliot Winkler is a Staff Software Engineer, Shared Libraries at MetaMask. This episode blends a diverse range of topics, from casual gaming to innovative testing approaches. They explore Elliot's transition to JavaScript and TypeScript at MetaMask while maintaining his Ruby roots through open-source projects like Shoulda Matchers. They also talk about the history and purpose of these matchers, their role in testing Ruby on Rails applications, and the intelligent diffing capabilities of Super Diff. So, sit back and get ready for a deep dive into the world of Ruby, testing methodologies, and a bit of fun along the way!

Socials

Picks


Become a supporter of this podcast: https://www.spreaker.com/podcast/ruby-rogues--6102073/support.

Transcript

Hey, folks, welcome to another episode of the Ruby Rogues podcast. This week, I'm your host, Charles max Wood, and I am talking to Elliott Winkler. Elliott, Now, do you want to introduce yourself real quick? Let people know who you are and why are we talking? Sure?

I can? Yeah, Yeah, my name is Elliott. I guess I've been I don't know, doing doing programming stuff, turn abub stuff for gosh, I don't know, like fifteen years at least, since I was in high school, I guess, really so, and yeah, I worked.

I worked at different consultancies and product companies, you know, doing doing wreading Ruby mainly in the past, but also like as a I don't know, a few years ago kind of writing more like uh diavascript typescript that kind of stuff, okay, but by kind of just doing full STUF in general, so uh huh so yeah, and then also maintaining some open source libraries on

the side A good deal. Yeah. I think we got this lined up because typically what I do is I have a virtual assistant that I'm like, hey, projects like this, So you know, if people are watching on the live stream, it says should a matchers, you know, we'll probably go into a few things here, but yeah, so I probably handed off and said, hey, I want someone to talk about should A matcher, So we'll probably go into that. But you know, yeah, it looks

like you're maintaining a couple of other open source libraries. I think we're also interesting to get into, and so we'll just kind of see where this goes. And anyway, it's it's always interesting to see where people are at. So, yeah, why did you start us off with just how you got into working on some of these projects like SHOULDA or you also mentioned super diff Yeah, yeah, sure, yes, I can start with SHOULDA. Yeah, so I worked at thought Bot, I think between two thousand and thirteen

and twenty sixteen. There we got it's a little while back, but but when I was but when I joined, you know, one of the things that they had and they might have that they might still have this now, but they kind of had like a twenty percent time sort of similar like how Google has or maybe they used to have. I don't know if they still have that now, but but yeah, every every I guess every other friday, we would you know, get a chance to work on something that was

valuable to the company. So so I saw that we had developed should A should have matters, should A context, those kind of this kind of triumph fire ate of of gems there. So yeah, that was interesting to me. So and I wanted to get more involved in open source. So I just began submitting poor requests, you know. And yeah, at the time, I think it was maintained by by two or three people, and they were you know, they were open to the to the contributions, and then

you know, they didn't really want to take it over anymore. I think they kind of were interested other things. So I was I volunteer, and I was like, okay, I'll take this over. And so I, you know, it just began kind of making more changes, responding to people you know that were uh, submitted bug reports or whatever. And yeah, and then I just kind of actually ended up maintaining that for quite a while, like even after I left that bot, probably you know, five or

so years after that or even longer, I'm not sure. I kept maintaining it just because it was just a fun thing to work on. Yep. Cool. So for those that aren't familiar with it, should A matchers just give you a convenient syntax for asserting things are the way you want them to be right, and it's usually a something should something else. And so anyway, that's kind of where the name comes from. Is that a fair summary you think? Or yeah, no, I can. Yeah, So trying

to think about the history a little bit of the project. So I think initially it was just our respect only, but I think now it was both. It does. But well I think I think even initially it was it was test unit. I want to say, really, yeah, there was a time so when I first joined thought but I think they were like going through a transition period where they were they were transitioning or maybe they had just transitioned from Testing unit to prospects okay and so so these and so they so

it started out with with should uh. I think that was like the original gym that came out and it was a way and so and and you know for for people to use testing it. You know it's like now they have well now, well, I say, now it's been a while. It's been it's been seven years, many years, but you know they have like test back and so you can write your tests just similar to respect. But I mean, but I but back then I believe that is in Texas even

new or maybe hadn't come out yet. So so you know, people thought about were like, what can we do to improve test units? So they made kind of a syntax on top of test unit and so you could and and so you could you could write your tests kind of in a more uh expressive way. And then and so that that's a macros. So it's like you could you could certainly, you know, you could have your contexts.

Uh, you know, you could have your IT blocks. But then you can also you could also define define some macros and so you could say, you know, should validate presence. It's been a while, I'll have to look at the right, but I believe, yeah, I believe that they would just define its like class methods within within like the test unit class or something like that. Then within your test class, I should say, uh

huh. And then later on ours came out and there and and so they started transitioning those two arspect matchers, and so they kind of broke apart, like the context part that was adding the syntax on top of and then that should and then the matchers that were first I think designed to work with ourspect and then I think we're added like support was added to also use them in

testing it as well. And so yeah, so the matchers were added, I think because they wanted to, you know, they wanted a way to you know, we want we wanted a way to just verify that associations work correctly, validations work correctly, you know, because all all the code you're adding to that, you're like, you know, when you add a validation to a model, Yes, you're using RAILS and you and you know RAILS gives you these nice ways DSL doing that obviously, right, code you're adding

to the model, like you need to test that code because some people have said, oh, well aren't you testing rails, Like when you when you're testing with these associations are validations and it's like, I mean, in some ways it is a layer on top of that, but also like you're the code you're adding is your code, that's that's kind of the custom to your your application, and so you need to test that. I so, and so it was kind of designed to fill in this gap right in a way

that that mimicked the syntax. So it's like you know, if you go and add you know, belongs to food or whatever, you know, to your U to your model or you know, has many whatevers, then the the corresponding test looks similar, so that you can kind of just easy. It's kind of easy to translate that a long description there, but but yeah, no, but it makes sense. Yeah. Well, and like on some of the code that I've been writing lately, I wrote some code around

the validations. I didn't use show to matches, right, and so when I when I pulled it in, it was okay, create a model with this, or you know, maybe I'm using another bought project factory bot, right, and so maybe I use the factory to generate it, right.

And so then I'm testing the validations and yeah, you know, I have to set up the model and then you know assert you know, model thought valid, question mark, and I have to do that over and over again as I kind of moved through things, and so yeah, it's a convenient way, you know. I came back to it and I was like, oh, this is kind of nice. I should be using it, but

I'm not. And so that was pretty convenient. But the other thing is is, especially on validations, so most of the associations are pretty straightforward, right. It either has it or it doesn't or has many of them or it doesn't right, and so you know, testing that that exists. Yeah, maybe that's not super helpful. I don't know. You get into scopes and then, you know, then it starts to get a little bit more

involved there with your associations, but with the validations. Some of the validations like presence or you know, things like that, yeah, you know, the name is either blank or it's not. But when you start getting into email formats or password length or password composition or password confirmation or things like that, right, then all of a sudden, the validation is a little bit

more involved. And so you know, having some of these matches kind of give you a convenient way of saying, no, I should match this, Yeah exactly, Yeah, I think most of I think, if anything,

it just help. It just gets you to start thinking about them. It's like, even if you have to kind of drop down to a lower level, right and if you have to test the validation yourself, it gets you to think about, like, oh, I should be testing these, And even if you do have to drop down, I mean, yes, we have you know, there are matchers for testing you know, basic about the validations that are built in the rails. But there's also Yeah, you mentioned,

you know, email email for writing. Let's say, you know, so let's say that you wanted to test that, you know, as long as there's an AD sign or something in the email address, then this email address should pass validation. Right, There's a there's a matcher called allow value that's in should have matches and so that that kind of lets you be a little more flexible, I guess with how your how you're testing. Yep,

absolutely, but yeah, it's kind of a convenient anyway. It's a convenient way to kind of get at least the fundamentals tested through so that you know what it's doing. Are there other things that the should the matchers do besides sort of the associations and validations. Yeah, so let's see. Yeah, so there's there's also matchers that relate to controllers. Oh right, yep, so you can you can test if you're writing controller trusts. I'm not you

know, uh, some people write them, some people don't. But you know, if you if you do, then you know, you can test things. You can you can verify you know, things that like you can

test that the response status is a certain thing. You can test that like maybe maybe you want to double check that certain values get added to the session or whatever like that, you can write that you can also test, uh, double check routes as well, So that is that's using so a lot of these matchers you just kind of built things that are built into rails anyway.

So so so the routes the routes is no exception there. But yeah, that's just double checking that certain routes are defined and that kind of thing. So yeah, cool. Yeah, it also is outlined and the if you look at the read me for showing yeah, there's there's a section on kind of what h the list of all the matches that are contained m yep.

So we also mentioned super diff and I'm going to ask you, Yeah, let's go into super Differ for a minute, and then I kind of want to talk about how these and maybe some of the other gems that you've used kind of inform your philosophy on what you test and what you don't how you test it. Sure, sure, so super diff So what's kind of the deal there. Yeah, so so this came out of, Yeah, it's interesting that you mentioned testing, because this does relate to that for sure.

So this kind of came out of some I guess frustrations I had with testing API responses specifically. So you know, let's say that you have some code that is that is making it, yeah, making any API requests, and you just want to double check that the response that comes back is a certain thing. So you can do that, but the response may be very large, and you know, our spect does give you a way like if you use what I guess whatever matter you use, but you could use equal

or whatever. It does give you a representation I guess a little bit of of like what's different between what you what you gave and what was actually the real API response. But it's not very easy to see. I think I think they just use a naive algorithm there to just convert. If I'm not mistaken, I think they convert the the object into a string or something and just do a simple string dift or something like that, right, and yeah, and so I just I just found, you know, just kind of

frustrating. Now is I had to spend some time looking through that dip to see or you know, what what you might do is like spit out the object and and then you visually compare it yourself. Just too a lot of time. So so I actually posted a tweet I think in twenty eleven. I just kind of posted it on Twitter and I was like, you know, you know, there should be a way to to do that. I

wish I could actually bring up the original tweet. I didn't. I didn't look to that well in the in the in the well, but yeah, I just kind of posted on Twitter. And then I think David from from ourspect he he he kind of posted a reply to that or retweeted it or something like that, and so I was like, oh, that's interesting, you know, he noticed, you noticed what I said, and so but so I kind of sat on that for a bit and I was thinking about

like how should I do this? And I think I made it even like a prototype or something, and it didn't quite work well and so so yeah, so I kind of didn't know what to do with it, and then I revisited it a few years later, I think, and thought of a new design for how for how that should work and so yeah, so the way that it works now is it will it will compute like an intelligent diff of the differences between two objects two data structures, so they could be hashes

they could be arraysed, they could be actual objects themselves. And then it gives you a diff and it and it descends recursively into the into the data structure. So it knows like if you're comparing two objects, it knows like, okay, and and the keys and the keys are the same, sorry, two hashes and the keys are the same. Then you know, it can see that, and I can tell you, okay, well the values

are different in this way. Or if it's an array, let's say that you're comparing it, it knows, okay, you've just shifted these values around or you know, whatever happens to me. And there's and there's specific algorithms for differ for certain kinds of objects. And then you can customize that the diffing algorithm, you know, if you have a you know, if you

have your own object or whatever. So yeah, so that way, so that yeah, So that that ended up helping a lot with with with this problem that I had with the A P A with with comparing the API responses, because it will it's it was able to highlight, you know, only the differences that I that I cared about in the in the in the expected versus the actual data and helped me kind of narrow down what I needed to change or what was wrong with my test or whatever whatever the case be.

Right, cool, So how does how does all of this kind of come together to inform how you test stuff? Yeah? I mean did you want to just know like kind of how like what's my approach to testing? Or yeah, yeah, go ahead and start there and then yeah, do you actually use these in your own day to day things? Yeah? I mean so, I guess, yeah, I mean when it comes to testing.

Yeah, so, like definitely on every Ruby project I've worked on, like I've I've always added this gym because because you know, even if it's even if even if I'm not adding working with API responses, because it just helps it just helps to know, like right what it helps to come of meet helped me to write test faster, I guess in a way because I don't have to spend so much time figuring out you know, what what the uh

what's wrong with the tests or what's wrong with the code. But yeah, in terms of you know, testing in general, I mean I I I write tests. I like tests. You know, sometimes there are pains, right, but they are very useful, you know, It's one of those things where I don't know, like and sometimes I'll write the test first and sometimes I won't, kind of depending on on the on the problem there. Sometimes I kind of like to do it the more exploratory thing and then kind

of come back and write test later, right. But but for the most case, for the most part, I you know, I tend to write my code and write my tests kind of at the same time. And and also I tend to make my pieces of code small so that writing reading the test for those pieces, you know, is easy as well. So I yeah, I guess I could go into more detail about like other other aspects, but I guess that's kind of like my general approach to testing, at

least at least unit testing. I mean, there's other kinds of testing as well. But yeah, cool, So you said that these days you're kind of working more in the typescript front end land as opposed to the ruby and rails and back end land. So so yeah, so what what's kind of the story there was? What's your history with all that? Yeah, I mean I've always been interested in in javascripts anyway, and you know, or all things front and I should say I never really shied away from you know,

travascript, CSS all that stuff. So I always enjoyed writing that and you know, with with with with JavaScript and type script in general. Yeah, I guess in past jobs, I never really you know, I was, yeah, I was kind of doing mixed I guess of of JavaScript and Ruby. And then yeah, this this this job I work out now. So I worked for a company called meta Mask. They're there, they're in the crypto space. But I'm not a you know, I'm not a crypto.

I'm not here to shield crypto or anything like that. But but I think I used them. At one point, I had a browser plug in for a wallet or something I can't remember. Yeah, yeah, it's yeah, it's it's a it's a well, yeah exactly, that's what it is. It's a browser plug in for a while. And yeah, and so

that's all written in JavaScript. So that's when that's been interesting because it's just you know, it's just it's a whole in the world, you know, different different ecosystem there, and and I mean the languages are have similar enough things to where the transition from you know, from Ruby to JavaScript isn't isn't so bad. There are some slightly different names for things. But but for

the most part, that's it's. It's it's. I found it pretty easy to kind of go back, go back and forth, you know if I

need to. So yeah, Well, though on occasion, you know, if I work on something and Ruby on the side, I might get you know, confused where it's like, let's say that you want to take a uh that's say that you want to convert something to Jason so right, you know Ruby, that's Chase on dot generate, but in shopscripts Chason dot string if I So, sometimes I'm mixed, right, but yeah, so yeah, I mean, I mean these days I pretty much code in uh in

typescript for my jobs. I don't do much Ruby, but I mean I still I still like writing Ruby, so I think it's a nice language. Yeah. So, and I still use some some for my side projects. Gotcha. So it looks like you're still maintaining some open source in Ruby. So how how does that balance out, I guess in your life and your career and what is it like, I guess if it's not your primary language these days, Yeah, I mean, I guess, I guess these days.

Yeah, these days it's just like you know, I I work on Well, I still you know, I still want to I still want to keep it up for sure. Right, It's like, you know, it's still a language I find useful, so I don't want to I don't want to forget how to use it. I'm definitely not looking to like kind of put it aside or anything like that. And so yeah, I still I still, I guess maintain side things. Yeah, I don't know, it's

still something that I enjoy using. I guess cool. Was there anything else you wanted to go into on the on the show or yeah, I don't know. I mean, I guess nothing. I guess nothing can come to mind from myself. But yeah, I don't know if there's any other topics that you wanted to and not on my list. So yeah, the way we kind of wrap the show up as we do picks. I don't know if you're familiar with picks, but we just shout out about stuff we like.

So I'm gonna throw out a few picks and then you can go ahead and do the same, and then we'll just wrap it up. The first pick that I'm going to throw out, I always do a board game pick or in this case of card game. And this is a card game that I play with my kids, specifically my youngest daughter. She's eight and you know, and so we she's starting to get into the more involved games.

But one game that we've played with her for the last few years that we got for her for Christmas when I think she was six, it's called Sleeping Queens two. Now there's a Sleeping Queens and then there's Sleeping Queens two, right, So in Sleeping Queens, you're trying to get kings and the kings go and you know, wake up the queens and rescue them. In Sleeping

Queens, too, it's the other way around. You're getting queens that are now rescuing the kings, and you know, the different decks have different cards, like to steal cards from other players, or put queens back to sleep or put kings back to sleep and things like that. So Sleeping Queens is a little simpler than Sleeping Queens too, but not terribly. You know, they're both pretty approachable for kids, I think they say they're eight eight years

plus. But we were playing it with her at you know, six or whatever, and she'd get this cackle every time she'd steal a king or whatever. But anyway, board game Geek has it rated as a one point five. Now, as I generally point out, a lot of your kind of complicated enough to be interesting but approachable for the general not Deep Gamer is about a two on the Board Game Geek weight. And so if I had to recommend it to people, it's a little simple to play with your adult friends,

it's about right to play with your kids. So anyway, it's it's fun game. It's like a twenty minute game, right, and so if you have an hour to kill, you can play two or three of them, right, or maybe even four of them. And so I'm gonna throw that one out there a couple of other things that I'm just gonna push as far as picks go. So lately I've been on my phone. I picked

up a new app that's my new thing that I'm into right now. I've been wanting to learn Japanese forever, and so I picked up dual Lingo on my phone. And I'm sure everybody's going, oh, du a lingo, right, I've used that. It feels like everybody knew about it before I did, but I didn't know that there it was free, right, you can pay to have Duo Lingo, super due a Lingo or whatever they call it. But anyway, I've just been using it for free and it's been

awesome. And so initially I picked it up and I started learning Japanese, right, and I'm still going through the ordering food the first lesson. Right when I was a kid, they taught us in elementary school to count to ten in Japanese. Right, so there you go, folks, I can say about eight food words and then like and and please and anyway. But yeah, so I'm gonna pick it up slowly, but I'm gonna pick it up, and I'm really enjoying it. But then I realized that one of

the places that I've always wanted to travel is to France. My grandmother was French. She immigrated to the US. She actually met my grandpa in Montreal. He grew up in Alberta, Canada. But yeah, so I've always wanted to go see some of these places, right. I don't know that I didn't necessarily go see anything that has a direct tie to my grandmother or my great grandmother in France, but just kind of to have that connection, right, And so I was like, okay, well, I took French

in high school, so I'll pick it up on Duolingo. So I added French, and so you know, every day I do a Japanese and a French. And then I was chatting with this guy online who I don't even remember how we got connected, but he's a member of the Church of Jesus Christ the Latter day Saints, as am I. And we were talking about church missions. And I served my church mission in not Japan, not France,

in Italy. And so I speak Italian fluently, especially if you want to talk about church, but my vocabulary is kind of thin in some other important areas. Right, I could go to the grocery store and buy food and not think about what I was trying to say, Right, And I could go out and I could talk to people about kind of lifestyle stuff in general, right, And I had the vocabulary for that, but I mean

there were some words that I just never learned, right. And so I was chatting with this guy and I was like, huh, I wonder if I could put this into duo lingo and make that work. And so I put it in and I basically said yeah, because let's just say, how how good are you at it? Right. So I dragged it all the way to I speak this fluently right, and I'll have to admit that the lessons are still pretty rudimentary. But my vocabulary has been expanding, which has

been fun. And so I learned words for like exercise, which was something that I had never right, I'd never had to say on my mission. And so anyway, that's been a ton of fun. So I've really really been liking Dual Lingo, and like I said, you can get it on I have it on my iPhone and you can use it for free, and it looks like you can use it for free forever. Now. I keep

getting ads in the app, saying and that's part of the deal. So I'll get the ads in the app for like, hey, play this pirate game or whatever right after I finish a lesson, and then as soon as that I click out of that, right it's like nope, nope, nope, nope. Then it plays the hey get super dual Lingo and make the ads go away, and I, you know, nope, nope, nope, nope. But so far as been awesome, So I've been enjoying that.

I try not to do the lessons back to back to back because I don't want to cross the streams with any of the languages, if that makes sense, And French and Italian are close enough, like a lot of the words are different, but the structure is very similar. What I'm finding in Japanese is they they structure their language different, very differently, and so that's

less of a thing. But anyway, so I'm gonna pick do a lingo, and since I talked about it for so long, I'm just gonna plug go check out Ruby Geniuses at rubygeniuses dot com if you want to be on a regular basis talking to people about Ruby and rails and that kind of stuff. You know, Elliott mentioned that he's doing JavaScript and typescript. I'm also starting up JavaScript Geniuses here pretty quick. So anyway, what are your picks? That's awesome, that's call. Yeah, I guess I don't have many

picks, but I just so. I recently went to a concert. So it was by this artist named JAKB. Collier, and he's just an artist I've been following for a while. If you may not already, if you're like in the music scene, but if you're not, it's kind of a

kind of interesting. I don't know, he's pretty I don't know, he's just a genius when it comes to when it comes to music, and you know, he has you can look up his like early YouTube videos and it's interesting how he can just combine his like different melodic ideas and different just late kind of layers vocals on top of each other and things like that. That was kind of his signature, I guess earlier on. And now he's kind

of expanded and did different musical genres and things like that. He's collaborated like all kinds of artists across different different things and so and he and he puts on a pretty great show as well. So you know, if it comes to your city, definitely definitely check him out. And you and you're you're and you're into music, of course check him out. Yeah, So you know that's that's that's my pick, I guess based on Yeah. All right,

well I'm gonna go ahead and wrap it up here. I know this is a little shorter episode than people used to, but that's totally fine. Thanks for coming, Elliott, Yeah, thanks for having me. All right, folks, we're gonna wrap it up till next time. Max Out

Transcript source: Provided by creator in RSS feed: download file
For the best experience, listen in Metacast app for iOS or Android