Thoughts About Unit Testing | Prime Reacts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right first off I mock your mocks this is by Lane if you don't know Lane boot.dev Great Stuff learn back end development by writing real code so it's like a site to learn stuff with you know what I'm talking about this is technically a sponsored read I want to say that right now okay but I get the opportunity to say what I want about it and I get to pick the topic and this is a great topic this is perfectly what I want to talk about because I have many many strong strong feelings about mocks yeah sponsored hashtag I gotta say it up front because I want to be an honest individual okay okay several years ago I started my first job as a senior go developer a lot of scare quotes going on here you see after a modest three years in the industry my Arcane ability to use go standard Library string contains function managed to leave a powerful impression on the hiring team yes okay so first off he may not realize what he's saying but there is something that's really good here which is know your language you dummies you walk into an interview you could just magic them with some really simple stuff that's just available because that's what's available in your language just know the things yep as a senior developer only a few years out of college with no kids and a six-figure salary life was looking pretty easy well it wouldn't have been easy it would have been easy if the mind-numbing gelatinous corporation goo known as Java it runs on billions of devices by the way it hadn't shipped away at the once great minds of my new colleagues Java by the way oh no oh no Corbett needs you to find the difference okay that's actually that's actually a really good meme this is a really good meme hard drugs free basic uh actually that's just straight banging heroin uh we're talking about my struggles with unit tests at this new company and Mox and heroin injection um I mean dependency injection to be honest they're both just as bad for you I am on the same team in general I am a huge huge advocate for never mocking I'm on team never mock okay okay never mocker right here okay uh before we do that let me tell you a quick story to give you some context on this new job fix and go I'm not 30 minutes into my new job still Homebrew installing a bunch of nonsense when a co-worker let's call him bill uh you know it's no Tom I'll tell you that much there's no Tom right now spins around says hey you're the new go guy right yeah uh goes the worst luckily for you I fixed goes biggest problem if someone actually says this like hey I fixed typescript's biggest problem I know immediately that whatever they fixed is probably worse than the thing itself I hate to say it but you're not making things better okay you're not you're not doing a good job Bill's the genius in this story I smell a Tom I smell a Tom I've re-added try catch the language no more if error equals nil welcome to Costco they say if your hand is bigger than your head you're likely to get cancer later in life by the way that was a joke from the 90s to get kids to put their hands on their faces and then you hit their hand and so that they hit their own face okay that's pretty much what you're doing right here this is the exact same joke except for it's in code and you don't get to hit your face once you get to do it every single time you program this is did that all the time everyone did it it was a great joke it worked every every time uh take a look at the Elegance of Bill's new feature funked try catch funked Panic airs new I'm scared Funk error error print caught wondering how Bill achieved this level of syntactic sugar super easy barely an inconvenience it just bastardized the Panic recover and defer keywords oh gosh the defer oh no oh no uh try catch Funk Funk catch Funk error error this oh my goodness that's even a named I hate naked returns this is the Devil by the way this is actually this is the biggest crime against humanity this is goe's worst feature naked returns are disgusting TJ was right about it absolutely TJ was right about it because you can't you have no clue what the f is going on negative turns are the devil our Titan or not they're tight with the devil okay uh running some tests after uh after a riveting discussion with Bill about the virtues of errors as values this is I mean I'm on Camp airs as values any day of the week I was able to dive into my first zero ticket this is uh this is correct this is 100 correct I need to implement a new feature on an existing and back-end go service I ran a few commands to get going get clone go build go test failed database connection connection refuse awesome darn these tests failed that's odd someone must have committed some broken code wait why are my unit tests trying to connect to a database server hey Bill do I need to set up a local postgre server to run the test yeah I didn't have time to write mocks for the database layer so we just use the real database I will I just want to take a really quick second to say one of the glories of sqlite is that you can point to a file even if the file doesn't exist and it can create a database right in that file and so therefore for this type of test if you truly just didn't want to mock anything you could have a file that just has the database in a specific condition and then just CP the file put it in a specific location and then run your test on it so that is one nice part but you know what if you're connecting to a database on a unit test you don't jump the shark okay I just I just want to let you know that right now your job is in a sun setting position you got to get the hell out of there uh the time has come to introduce you to my philosophy on unit test well at least in as much as unit test related to databases we are a back wait we are back-end developers 50 of the code we write takes data from HTTP requests fiddles with it a bit and then saves it to a database the other 50 of our uh codes takes data from the database does some fiddling and sends it back to the client in the HTTP response you know give or take a few percenties genuinely fairly well done uh here's my hot take the fiddling bit is the only part worth unit testing how many times have I said this you always have the fiddling part as its own function that takes in the thing you wish to you wish to diddle you do the diddling and you return the diddled like there's no other dude stop stop having functions that both get data and did all the data okay it's not worth it and yes I'm saying did will not fiddle because it's funnier okay uh it is shocking how often people don't do that okay it is shocking that that's not a real thing my time in seminary uh it was about diddling what I I don't even know why I read that uh here's where I really disagree with Bill I actually prefer his tests that run while connected to a real database though I prefer let's see though though I'd more prefer integration tests that runs separately from the unit test at least with the real database I'm testing code that actually runs in production mocks are perhaps the worst thing uh one of the worst things to happen to back-end development it's actually just the worst thing to happen to all development not just back-end development because here's the problem it gives you all the power to do integration tests in unit tests so you end up testing just the stupidest stuff in the universe and don't say and don't forget we allow JavaScript to run on the server side yeah I like Lane I like Lane I hate mocks because they don't test code that actually runs in production which means they don't test anything give a delightfully false sense of security management loves this these are all facts these are all facts these are all facts I don't like the song that's playing it's way too intense we got we gotta change the song immediately I am not in love I am not in love right now all green no red Always Green all right take the following function uh DB squeal user if email address equals null okay so I can already tell why you've heft up okay see this line right here this line should be in a this should this should all be in different function right there this is the only thing that should be in this function right this is it this is the only thing that should be in this function the Rest by the way look it in look at this guy just raw dog and a squeal look at that can you believe there's raw dog squeal right now like I mean how would anyone ever raw dog squeal it's impossible right he needs an orm immediately all right so save user okay there's no way to test this function without a database connection available at the time of testing I can practically hear Bill Brew installing postgres as I type my humble argument is that the database logic this part right um should uh be tested in an integration test or gasps manually I'm fine with that however this validation logic can easily be tested in a unit you know Yep this part yep absolutely me and Lane are like notice we're on the same page uh we by the way lane and I had like an hour-long talk and part of our hour-long talk involved us just hating mocks just for like an excessive period of time so I'm not shocked at all that this this somehow came out as as an article it's critical to remember one of the key tenets of software engineering your code is ass it's facts and because your code is asked you shouldn't be scared to make changes sure this function will require database connection to be tested uh but what if you break it down what should we call then units yeah the classic your code is ass problem classic really ah now we have to write a neat little test test user yep awesome we've we've tested all the validation logic uh we would have tested in the original function with a mock and now we don't need to you know write a stupid mock absolutely mocks cause blocks blocks cause talks talks about mocks with devs or Cox what bugs with shrugs come bugs with lugs come lugs with bugs with Mighty shrugs come look here look here Mr Dev dear let's stop the mocks that cause the blocks here we can test and we can Nest here but we don't need to mock the rest here probably chat chipity but the Dr Seuss is beautiful okay mocking is akin to reading Dr Seuss your mouth does a bunch of movements that your brain can't quite understand at all this was most excellent I love this I am on I'm full on this team by the way this is a full on team to stop mocking right better back end code separate out your logic can we all give little claps for uh for Lane this was a great article uh again this was sponsored but still I actually really like the article check out boot.dev boot.dev helping your back end Journey okay and Lane's actually he's he's an awesome engineer I would not promote this if I thought Lane was a dummy he's not a dummy he's significantly better than me at back end engineering listen to him plus notice he made the joke JavaScript on the back ends the joke and that's hilarious okay we could all appreciate that because the name is Javascript on the back end is a joke again
Info
Channel: ThePrimeTime
Views: 212,869
Rating: undefined out of 5
Keywords: programming, computer, software, software engineer, software engineering, program, development, developing, developer, developers, web design, web developer, web development, programmer humor, humor, memes, software memes, engineer, engineering, Regex, regexs, regexes, netflix, vscode, vscode engineer, vscode plugins, Lenovo, customer service
Id: KzV0mTqBcZA
Channel Id: undefined
Length: 11min 20sec (680 seconds)
Published: Wed Jul 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.