Latest / CyberCode Academy / Course 37 - Building Web Apps with Ruby On Rails | Episode 12: Comprehensive Rails Integration Testing
Transcript
- 0:00Speaker 1: Imagine you spend months building a custom car. You test
- 0:05Speaker 1: the engine on a block and it just purs.
- 0:07Speaker 2: Perfectly right the ideal scenario.
- 0:09Speaker 1: Yeah, exactly. You test the steering column on a work bench,
- 0:12Speaker 1: smooth as silk, that breaks grip tight in the vice.
- 0:15Speaker 1: Every single component passes inspection with flying colors. But then
- 0:19Speaker 1: you bolt it all together, sit in the driver's seat,
- 0:22Speaker 1: turn the key, and the steering wheel immediately falls off,
- 0:25Speaker 1: while the horn blarares Huh.
- 0:26Speaker 2: Yeah, it happens way more often than anyone wants to admit. Actually,
- 0:30Speaker 2: Welcome to the frustrating, fascinating gap between theory and reality
- 0:36Speaker 2: in software development.
- 0:37Speaker 1: Welcome to the deep dive. Today. You and I are
- 0:40Speaker 1: putting on our diagnostic hats. We're digging into system architecture,
- 0:43Speaker 1: developer documentation, and honestly, some brutally honest post mortem software
- 0:48Speaker 1: disaster reports.
- 0:49Speaker 2: Oh the disaster reports are always the best part.
- 0:51Speaker 1: They really are. And our mission today is to walk you,
- 0:54Speaker 1: step by step through the exact mechanics of how to
- 0:57Speaker 1: test a modern web application thoroughly before a single member
- 1:01Speaker 1: of the public ever touches it.
- 1:03Speaker 2: Because you know, knowing your individual lines of code work
- 1:07Speaker 2: is a completely different universe from knowing your actual application
- 1:11Speaker 2: works as a cohesive unit.
- 1:13Speaker 1: Right, So let's set the scenario for you. Imagine you
- 1:16Speaker 1: are the lead developer on a brand new dating app.
- 1:18Speaker 1: Launch day is looming. Now you've been really diligent. You
- 1:22Speaker 1: already have your basic unit tests written and passing.
- 1:25Speaker 2: Which is great. It's step one exactly.
- 1:28Speaker 1: That means you've independently verified that the registration button functions,
- 1:32Speaker 1: the login page accepts passwords, the user profile page renders.
- 1:36Speaker 1: In isolation. The pieces are rock.
- 1:38Speaker 2: Solid, and a lot of developers just stop right there,
- 1:41Speaker 2: dust off their hands, and deploy. But unit testing alone
- 1:45Speaker 2: provides this massive false sense of security.
- 1:47Speaker 1: I want to push back on that a little bit though,
- 1:49Speaker 1: because if the engine works and the brakes work, why
- 1:51Speaker 1: wouldn't the car drive? Like, what is the actual blind
- 1:54Speaker 1: spot there?
- 1:54Speaker 2: Well, it comes down to the philosophical difference in how
- 1:57Speaker 2: you are interrogating the machine. Unit tests are inherently about isolation.
- 2:02Speaker 2: You trap a tiny piece of logic in a sterile
- 2:04Speaker 2: vacuum and basically ask, did you do your one specific
- 2:07Speaker 2: job right?
- 2:08Speaker 1: Just that one job?
- 2:09Speaker 2: Yeah, But your users don't live in a sterile vacuum.
- 2:12Speaker 2: They live in the messy handoffs between those pieces. What
- 2:15Speaker 2: happens if a user registers, gets redirected logs in, checks
- 2:19Speaker 2: their shiny new dating profile, and tries to edit their
- 2:22Speaker 2: bio all in one frantic, unbroken, continuous session.
- 2:26Speaker 1: Ah. So, the individual parts might execute, but they might
- 2:29Speaker 1: be speaking completely different languages when they try to hand
- 2:32Speaker 1: the data off to each other.
- 2:33Speaker 2: Exactly. That is where the most dangerous app breaking bugs
- 2:37Speaker 2: hide the transitions. To find them, you have to leave
- 2:41Speaker 2: the vacuum behind and build what we call an application
- 2:44Speaker 2: flow test or an integration test.
- 2:46Speaker 1: An integration test okay.
- 2:47Speaker 2: Yeah, you're basically simulating an invisible automated user. Just run
- 2:51Speaker 2: and wild through your application.
- 2:53Speaker 1: All right, let's actually build this out. You're sitting at
- 2:55Speaker 1: your terminal. To get started, you run a basic generator
- 2:58Speaker 1: command in your framework. You type something like rails generate integration.
- 3:03Speaker 2: Test user flow right standard setup.
- 3:05Speaker 1: The terminal chugs for a second, and it spits out
- 3:08Speaker 1: a blank slate, just an empty file with a basic
- 3:10Speaker 1: connection assertion to prove the testing engine is running. You
- 3:14Speaker 1: run it and you get a green light.
- 3:16Speaker 2: So far, so good. Yeah, now you need to instruct
- 3:18Speaker 2: your automated test user to perform their very first real
- 3:22Speaker 2: world action, which is registering for the dating app. Okay,
- 3:26Speaker 2: you write a line of code that sends a block
- 3:28Speaker 2: of registration data like name, email, password, directly to the
- 3:32Speaker 2: app's creation route.
- 3:33Speaker 1: Now I'm looking at the disaster reports we gathered, and
- 3:35Speaker 1: this is where developers hit their first wall. You send
- 3:38Speaker 1: the data, the application processes, it creates the user, and
- 3:42Speaker 1: sends back a response saying success.
- 3:45Speaker 2: But then, Lenny, guess it freezes.
- 3:47Speaker 1: Yes, it halts completely. The automated user just stands there
- 3:50Speaker 1: staring at the success message. Why does it do that?
- 3:53Speaker 2: To understand the freeze, you really have to understand the
- 3:55Speaker 2: fundamental architecture of the Internet. The Web is inherently.
- 3:58Speaker 1: Stateless, meaning what exactly break that down?
- 4:01Speaker 2: Meaning the server has no memory of the conversation you're having.
- 4:05Speaker 2: Every single time a browser asks a server for something,
- 4:08Speaker 2: it's treated as a completely isolated, brand new interaction.
- 4:12Speaker 1: Oh weird.
- 4:13Speaker 2: Okay, Yeah, the server processes the request, sends a response,
- 4:16Speaker 2: and immediately forgets you exist. So when your dating app
- 4:20Speaker 2: processes that registration, it sends back a specific HTTP response
- 4:24Speaker 2: that essentially says I created your account. Now go redirect
- 4:28Speaker 2: yourself to the homepage to log in and a.
- 4:30Speaker 1: Normal web browser like Chrome or Safari, reads that redirect
- 4:34Speaker 1: your self command automatically fetches the new URL and loads
- 4:37Speaker 1: the homepage seamlessly.
- 4:38Speaker 2: Exactly You the human, don't even notice it happening. It's
- 4:41Speaker 2: so fast.
- 4:42Speaker 1: But your automated test is not a web browser. It's
- 4:44Speaker 1: just a highly literal, dumb piece of software.
- 4:46Speaker 2: Precisely it receives the redirect response, reads success, and says, great,
- 4:50Speaker 2: my job is done here. It doesn't know how to
- 4:53Speaker 2: follow a map unless you explicitly force it to.
- 4:55Speaker 1: It's like telling someone to drive to the grocery store.
- 4:58Speaker 1: They get to the parking lot, put the car in park,
- 5:00Speaker 1: and just sit there forever because you didn't explicitly write
- 5:03Speaker 1: a command telling them to open the door and walk
- 5:05Speaker 1: inside the building.
- 5:06Speaker 2: That's a perfect visualization. Actually, to solve this, you have
- 5:10Speaker 2: to introduce a crucial tool into your code called follow redirect.
- 5:14Speaker 1: Okay, follow redirect right.
- 5:16Speaker 2: It's a command that forces your testing environment to temporarily
- 5:20Speaker 2: mimic a browser's behavior. You tell the test do not
- 5:24Speaker 2: stop at the first response, read the destination URL in
- 5:28Speaker 2: the header, and execute a brand new request to load
- 5:31Speaker 2: that next page.
- 5:32Speaker 1: Awesome, so you drop follow redirect into the test script.
- 5:36Speaker 1: You run it again and boom, your invisible user lands
- 5:38Speaker 1: safely on the log in page. The registration handoff works,
- 5:42Speaker 1: which is.
- 5:42Speaker 2: A great feeling. But now you need to handle the
- 5:44Speaker 2: log in itself and testing the happy path. You know
- 5:47Speaker 2: where everything goes, right, That's only half the job, right.
- 5:49Speaker 1: You want to test the valid user first. You feed
- 5:51Speaker 1: the test the exact email and password you just created.
- 5:55Speaker 1: You assert that the login is successful. But then you
- 5:58Speaker 1: have to act maliciously.
- 5:59Speaker 2: Yeah, you have to act like a user who forgot
- 6:01Speaker 2: their password or maybe a bot try and guess one.
- 6:04Speaker 1: So you script your automated user to feed the login
- 6:07Speaker 1: page complete garbage credentials. You want to verify that the
- 6:10Speaker 1: app rejects them and pops up an error message on
- 6:12Speaker 1: the screen saying like invalid password exactly. But wait, I'm
- 6:16Speaker 1: trying to picture how this actually works under the hood.
- 6:19Speaker 1: Your automated test doesn't have human eyes. It can't look
- 6:21Speaker 1: at a glowing monitor and read the red text popping up.
- 6:25Speaker 1: So how does it definitively prove that the error message exists?
- 6:29Speaker 2: It relies on reading the underlying matrix. The test engine
- 6:33Speaker 2: can't see the visual rendering, but it has complete access
- 6:36Speaker 2: to the raw HTML code that the application sends back.
- 6:39Speaker 1: Okay, the raw code.
- 6:40Speaker 2: Yeah, And to interrogate that HTML you use a function
- 6:44Speaker 2: often called a cert select in a lot of these frameworks.
- 6:47Speaker 1: Let's break down how you actually write that query.
- 6:49Speaker 2: Sure, so, in a sert select function requires two pieces
- 6:52Speaker 2: of information. First, the element query like what structural part
- 6:55Speaker 2: of the web page are you actually looking for? And
- 6:58Speaker 2: second the assertion what specific data do you expect to
- 7:02Speaker 2: find inside it?
- 7:03Speaker 1: Give me a concrete example from our dating app scenario.
- 7:06Speaker 2: Okay, let's say you want to prove your malicious user
- 7:08Speaker 2: was completely denied access and is still staring at the
- 7:11Speaker 2: public homepage. You know your homepage always has a big
- 7:14Speaker 2: header two tag in H two HTMIL element that says
- 7:18Speaker 2: welcome please log in.
- 7:19Speaker 1: Got it?
- 7:20Speaker 2: You write your assert select command to scan the entire
- 7:23Speaker 2: HTML document for an H two tag. Then you provide
- 7:26Speaker 2: the exact string of text welcome please log in. Oh
- 7:29Speaker 2: I see if the test scans, the code finds that
- 7:32Speaker 2: specific tag and the text matches perfectly. The test passes,
- 7:36Speaker 2: your automated user is officially locked out.
- 7:38Speaker 1: Okay, that makes total sense. You navigate via the struck
- 7:41Speaker 1: un role tag. So your registration works, your login works,
- 7:44Speaker 1: your security rejections work. It's time to test the core.
- 7:47Speaker 2: Experience the fun part, right.
- 7:49Speaker 1: You script the test to log in as your valid user,
- 7:51Speaker 1: hit the homepage and view all the potential dating candidates.
- 7:55Speaker 2: You run the test, and the whole system crashes.
- 7:57Speaker 1: Spectacularly, just a complete, unha handled explosion. And this brings
- 8:03Speaker 1: us to a massive point of confusion. I see all
- 8:04Speaker 1: the time in these developer notes the critical distinction between
- 8:08Speaker 1: a test failure and a test error.
- 8:10Speaker 2: Oh yeah, people use them interchangeably all the time, but
- 8:14Speaker 2: they are radically different beasts night and day.
- 8:17Speaker 1: So explain the difference. What's a failure?
- 8:19Speaker 2: A failure means your code successfully executed from start to
- 8:22Speaker 2: finish without crashing, but it produced a logical result you
- 8:26Speaker 2: didn't expect. Okay, it's like asking a calculator what two
- 8:29Speaker 2: plus two is and it confidently prints out five. The
- 8:32Speaker 2: circuitry works fine, but the math is wrong.
- 8:35Speaker 1: So what is an error?
- 8:36Speaker 2: Then? An error means the application or the testing framework
- 8:39Speaker 2: itself encountered a condition so impossible that it had to
- 8:43Speaker 2: panic and shut down entirely. To use the calculator analogy,
- 8:47Speaker 2: the calculator didn't give you a five. The calculator literally
- 8:50Speaker 2: caught fire on your desk.
- 8:51Speaker 1: Wow, which is exactly what just happened to our dating
- 8:54Speaker 1: app test. You look at the console and you see
- 8:56Speaker 1: an error thrown by the app's internal controller. Thing is,
- 9:00Speaker 1: the error had nothing to do with the login process
- 9:02Speaker 1: you were trying to.
- 9:02Speaker 2: Test well, And that's the beauty of integration tests. They
- 9:06Speaker 2: expose the collateral damage of your architecture. So what actually
- 9:09Speaker 2: caused the crash? In this case?
- 9:11Speaker 1: You dig into the stack trace and you realize the
- 9:14Speaker 1: app crashed because it was trying to load the profile
- 9:16Speaker 1: images for all the dating candidates on the homepage. But
- 9:20Speaker 1: in a test environment, you're using fake automated accounts. Those
- 9:24Speaker 1: fake accounts don't have jpeg images uploaded to a database,
- 9:28Speaker 1: so the application code blindly tried to fetch image URLs
- 9:31Speaker 1: that didn't exist, didn't know how to handle the empty response,
- 9:34Speaker 1: panicked and took the whole system down with it.
- 9:36Speaker 2: Right, and noticed that the solution here isn't to fix
- 9:39Speaker 2: your test. Your test actually did its job by uncovering
- 9:44Speaker 2: a fragile piece of application logic.
- 9:46Speaker 1: That's a great point. You actually have to go into
- 9:48Speaker 1: the app's core code and write a safety net. You
- 9:50Speaker 1: add a simple condition to the controller check if an
- 9:53Speaker 1: image file actually exists before you try to render it
- 9:55Speaker 1: on the screen.
- 9:56Speaker 2: Yeah, you add that robust check. The error vanishes, the
- 9:59Speaker 2: calculator stops cat fire, and your desk can proceed.
- 10:02Speaker 1: But the relief doesn't last long, does it. Now that
- 10:04Speaker 1: the page actually loads, you want to verify the data.
- 10:08Speaker 1: Those dating profile cards on the homepage are built using
- 10:11Speaker 1: a popular JavaScript framework called viewjs. Oh boy, yeah, they
- 10:15Speaker 1: render dynamically. So you tell your test scan the page
- 10:19Speaker 1: and count the number of user cards. There should be five.
- 10:21Speaker 1: You run it and it fails. The test confidently tells
- 10:25Speaker 1: you there are zero cards on the screen.
- 10:26Speaker 2: Ah, the JavaScript illusion this is. This is probably one
- 10:31Speaker 2: of the most common pitfalls in modern testing.
- 10:33Speaker 1: I would be pulling my hair out at this point.
- 10:35Speaker 1: I mean, if you open a real browser and look
- 10:37Speaker 1: at the app, the five cards are sitting right there.
- 10:39Speaker 1: Why is the test insisting the page is empty.
- 10:41Speaker 2: It's because of this shift in how modern web applications
- 10:44Speaker 2: are built. Ten years ago, servers did all the heavy lifting.
- 10:48Speaker 2: They would assemble the entire web page, bake all the
- 10:51Speaker 2: data into the HTML, and send a finished product.
- 10:53Speaker 1: To your browser right, and your increation test reads the
- 10:56Speaker 1: finished HTML perfectly exactly.
- 10:59Speaker 2: But today, for the sake of speed and interactivity, we
- 11:02Speaker 2: use client side rendering frameworks like View or React.
- 11:06Speaker 1: So what is the server actually sending back now? If
- 11:08Speaker 1: not the finished page.
- 11:09Speaker 2: It sends back a skeletal, almost empty HTML frame accompanied
- 11:14Speaker 2: by a massive bundle of JavaScript. When a human opens Chrome,
- 11:18Speaker 2: Chrome downloads that skeleton, executes the JavaScript, talks to the
- 11:22Speaker 2: database in the background, and dynamically draws those five cards
- 11:26Speaker 2: onto the screen in real time.
- 11:28Speaker 1: Oh, I see the problem. Our automated integration test isn't Chrome.
- 11:32Speaker 1: It doesn't have a JavaScript engine running exactly.
- 11:35Speaker 2: It catches that initial skeletal HTML response and immediately starts
- 11:38Speaker 2: scanning it. The JavaScript hasn't run, the database hasn't been queried.
- 11:42Speaker 2: The cards literally do not exist yet in the code
- 11:45Speaker 2: the test is holding. It is entirely blind to anything
- 11:48Speaker 2: rendered client side.
- 11:49Speaker 1: So if the test can't see JavaScript, how do you
- 11:51Speaker 1: verify the page loaded correctly without rewriting your entire app architecture?
- 11:56Speaker 2: You have to compromise on your coordinates. Instead of querying
- 11:59Speaker 2: the dynamically generated cards, you look for the anchor point.
- 12:02Speaker 2: You query the static user container the emptyhtmail box that
- 12:06Speaker 2: the JavaScript is posed to drop the cards into eventually.
- 12:08Speaker 2: Oh club, Yeah, that container is always present in the
- 12:11Speaker 2: raw skeletal HTML. You write an assert select to verify
- 12:16Speaker 2: the container exists, and your test passes.
- 12:18Speaker 1: It feels a little like checking if the garage is
- 12:21Speaker 1: built instead of checking if the car is parked inside it.
- 12:24Speaker 1: It's a compromise, but it keeps the test moving exactly.
- 12:27Speaker 1: So let's leave the user view and jump over to
- 12:29Speaker 1: the admin dashboard. You want your test to verify the
- 12:32Speaker 1: master list of users in the back end. You know
- 12:34Speaker 1: you created exactly five test accounts. You write a query
- 12:38Speaker 1: expecting to find exactly five rows in the HTML table.
- 12:41Speaker 1: You run the test. It fails. The test says there are.
- 12:43Speaker 2: Six rows another classics.
- 12:45Speaker 1: Nare I'm looking at this thinking did the system generate
- 12:48Speaker 1: a ghost user? But no, it's the header row, the
- 12:51Speaker 1: topper of the table that says name, email, join date
- 12:54Speaker 1: in HTML that is structurally considered a row.
- 12:57Speaker 2: Yeah, and this force is a philosophical decision you make
- 12:59Speaker 2: constantly in software engineering. Do you alter the application code
- 13:02Speaker 2: to accommodate the test, or you alter the test to
- 13:05Speaker 2: reflect reality.
- 13:06Speaker 1: Well, in the case, of the missing images. Earlier, you
- 13:09Speaker 1: change the application because it was fragile. But here the
- 13:12Speaker 1: header row isn't a bug. It's a permanent, intentional piece
- 13:15Speaker 1: of the UI. So you alter the test. You change
- 13:18Speaker 1: your expectation from five rows to six, and it passes.
- 13:22Speaker 2: Which brings up the challenge of navigation. If you have
- 13:24Speaker 2: a table with six rows and you only want to
- 13:27Speaker 2: interrogate the data of the third user, how do you
- 13:29Speaker 2: single them out?
- 13:30Speaker 1: You use CSS selectors. It's essentially a way of writing
- 13:34Speaker 1: a highly specific mailing address for a single element on
- 13:37Speaker 1: a page. You can use selectors like first child to
- 13:40Speaker 1: grab the top row or a last child to grab
- 13:42Speaker 1: the botto row.
- 13:43Speaker 2: And this is where developers just want to smash their keyboards.
- 13:46Speaker 2: Let's talk about the indexing heresy.
- 13:48Speaker 1: Oh man, don't even get me started. If you have
- 13:50Speaker 1: ever taken a single day of a programming class, what
- 13:53Speaker 1: is the golden unbreakable rule of lists?
- 13:56Speaker 2: A raise started zero? The memory offset of the very
- 13:59Speaker 2: first item is zero. The second item is item one.
- 14:02Speaker 1: It is a fundamental law of computer science. So you're
- 14:06Speaker 1: writing your test and you want to grab the second
- 14:09Speaker 1: paragraph of a user's profile to check their bio. You
- 14:12Speaker 1: use the CSS selector nth child. You type nth child one,
- 14:17Speaker 1: fully expecting it to grab the second paragraph because of
- 14:19Speaker 1: zero indexing. You run the test.
- 14:21Speaker 2: It fails, Of course, it does.
- 14:23Speaker 1: It grab the wrong paragraph. Why because in the bizarre
- 14:27Speaker 1: world of CSS, indexing starts at one. One is the
- 14:31Speaker 1: first item, two is the second item.
- 14:33Speaker 2: You really have to remember the history of the tools
- 14:35Speaker 2: you're using here. Programming languages like C or Python were
- 14:39Speaker 2: built by engineers managing literal physical memory addresses. Zero meant
- 14:43Speaker 2: zero distance from the starting block. But CSS wasn't built
- 14:47Speaker 2: for memory management. It was built for document formatting. It
- 14:50Speaker 2: was designed to mimic human publishing. When a designer says
- 14:53Speaker 2: make the first paragraph bold, they mean paragraph one, not
- 14:56Speaker 2: paragraph zero.
- 14:57Speaker 1: I mean it makes historical sense. But when you are
- 14:58Speaker 1: writing a Ruby or pathon TI has script and suddenly
- 15:01Speaker 1: you have to switch your brain to one indexed CSS
- 15:04Speaker 1: logic in the middle of a line of code, it
- 15:06Speaker 1: will trip you up every single time.
- 15:08Speaker 2: It absolutely will. But once you embrace the heresy and
- 15:11Speaker 2: change your selector to nth child two, you can pinpoint
- 15:14Speaker 2: your target.
- 15:15Speaker 1: Kin Pointing brings its own dangers, though precision is incredibly brittle.
- 15:20Speaker 1: You write a query to check the brand new user
- 15:22Speaker 1: you just created at the very bottom of the admin list.
- 15:25Speaker 1: This new user hasn't set a display name yet, so
- 15:27Speaker 1: you expect the name field to be an empty string.
- 15:29Speaker 1: You run the test, it fails. The console spits out
- 15:32Speaker 1: this angry message expected empty string found destroy.
- 15:37Speaker 2: I love this particular bug. It perfectly highlights how literal
- 15:40Speaker 2: machines are.
- 15:41Speaker 1: You trace back your steps. You wrote a CSS selector
- 15:44Speaker 1: to grab the very last row in the table, and
- 15:47Speaker 1: then you told it to grab the very last element
- 15:49Speaker 1: inside that row, assuming the name was at the end right.
- 15:51Speaker 2: But if you look at a standard admin dashboard, the
- 15:54Speaker 2: name is usually the first column. What is the very
- 15:56Speaker 2: last column on the far right of the screen.
- 15:58Speaker 1: A giant red button that says destroy to delete the account.
- 16:02Speaker 1: Your automated test did exactly what you told it to do.
- 16:05Speaker 1: It flawlessly navigated to the last element, scrape the text
- 16:09Speaker 1: off the deletion button, and assumed the user's legal name
- 16:12Speaker 1: was literally destroy. You are navigating blindfolded, relying purely on coordinates,
- 16:18Speaker 1: and if the layout shifts by a single pixel, your
- 16:20Speaker 1: test grabs the wrong.
- 16:21Speaker 2: Data, which is why relying on complex coordinate mapping is
- 16:25Speaker 2: a terrible long term strategy for testing.
- 16:27Speaker 1: So let's talk about the better way. You try to
- 16:29Speaker 1: verify the user's email address on their profile page. The
- 16:32Speaker 1: test fails because your query finds two different things, the
- 16:36Speaker 1: text label that actually says email and the data field
- 16:39Speaker 1: next to it containing the address. You could write a
- 16:42Speaker 1: crazy complex CSS query to navigate down through the parent divs,
- 16:46Speaker 1: skip the label, and isolate the data field.
- 16:48Speaker 2: But you shouldn't. Don't write a tangled MESSI test query
- 16:51Speaker 2: just to compensate for messi generic HTML. This is where
- 16:56Speaker 2: accessibility meets testing.
- 16:57Speaker 1: Oh, this is a great point. You go into the
- 16:59Speaker 1: applications action codebase and you add a clear, highly specific
- 17:03Speaker 1: class name directly to the data field. You type class
- 17:05Speaker 1: user email data.
- 17:07Speaker 2: And this solves two massive problems simultaneously. First, your integration
- 17:12Speaker 2: test becomes incredibly simple and robust. You don't need brittle
- 17:16Speaker 2: coordinates anymore. You just tell the test find the thing
- 17:19Speaker 2: called user email data. Even if the designer completely moves
- 17:22Speaker 2: the field to the other side of the page tomorrow,
- 17:25Speaker 2: the test still passes because the class name travels with it.
- 17:28Speaker 1: It's tethered to the actual data. And the second benefit you.
- 17:31Speaker 2: Just drastically improve the accessibility of your application. Screen readers
- 17:36Speaker 2: the software used by visually impaired users to navigate the web.
- 17:40Speaker 2: They operate on the exact same principles as your automated test.
- 17:43Speaker 1: They are non visual pursers exactly.
- 17:45Speaker 2: They rely on clear, semantic class names and tags to
- 17:48Speaker 2: understand the structure of the page and read it aloud logically.
- 17:52Speaker 2: By making the app easier for your invisible test robot
- 17:55Speaker 2: to read, you fundamentally made it easier and better for
- 17:58Speaker 2: human beings to read.
- 17:59Speaker 1: Is fantastic architectural synergy. Okay, we are in the home stretch.
- 18:03Speaker 1: Your automated user has registered, handled readerrects, logged in, navigated
- 18:08Speaker 1: the HTML matrix, survived the JavaScript illusion, and bypassed the
- 18:12Speaker 1: CSS indexing traps.
- 18:13Speaker 2: Rather than busy very.
- 18:15Speaker 1: The final piece of the continuous journey updating data. You
- 18:19Speaker 1: want your user to change their profile name right, and.
- 18:22Speaker 2: To do this in a test, you have to simulate
- 18:25Speaker 2: a specific type of HTPP request. You simulate a patch request.
- 18:29Speaker 1: Break that down for me, y patch.
- 18:31Speaker 2: Well, when you load a web page to read an article,
- 18:34Speaker 2: your browser is sending a get request. You are asking
- 18:37Speaker 2: the server to get data. When you submit a massive
- 18:40Speaker 2: form to create something brand new. That's usually a post request.
- 18:44Speaker 1: Okay, get in a post.
- 18:45Speaker 2: But when you are taking an existing database record like
- 18:48Speaker 2: your dating profile, and you just want to update a
- 18:50Speaker 2: single field, like changing your name from John to Jonathan,
- 18:53Speaker 2: you send a patch request. You are literally patching the
- 18:56Speaker 2: existing data with new information.
- 18:58Speaker 1: Makes sense. You script the Patrick tqus in your test.
- 19:01Speaker 1: You bundle up the new name Jonathan, target the update
- 19:04Speaker 1: route and assert that the response is successful. You run
- 19:07Speaker 1: the test. It fails. The aerologue is screaming about an
- 19:10Speaker 1: undefined method.
- 19:12Speaker 2: So what did the documentation reveal this time?
- 19:14Speaker 1: A simple humiliating grammar mistake. The testing framework requires a
- 19:19Speaker 1: past tense assertion when checking if an update triggered a redirect.
- 19:23Speaker 1: I wrote a seart direct instead of a search directed
- 19:25Speaker 1: to Pelto. It is always the tiny syntax rules that
- 19:28Speaker 1: completely derail you.
- 19:29Speaker 2: The machine takes you literally.
- 19:32Speaker 1: Every single time you fix the typo, you run it again.
- 19:35Speaker 1: The syntax is correct, the pat request is successful, the
- 19:38Speaker 1: database updates the name. But and there's always a butt.
- 19:42Speaker 1: When your test tries to verify that the new name
- 19:45Speaker 1: Jonathan is actually displaying on the screen, it fails again.
- 19:49Speaker 1: It can't find the name anywhere why think back.
- 19:51Speaker 2: To the very beginning of the journey. What happens after
- 19:54Speaker 2: a successful data submission.
- 19:56Speaker 1: Oh man, the server sends a success message and tells
- 19:58Speaker 1: you to redirect yourself back to the profile page to
- 20:01Speaker 1: see the changes. I forgot the command I forgot follow
- 20:03Speaker 1: redirect exactly.
- 20:04Speaker 2: The database successfully updated, but your test didn't follow the map.
- 20:08Speaker 2: It was just staring at a blank intermediate loading screen
- 20:11Speaker 2: frantically searching for the name Jonathan, while the actual fully
- 20:15Speaker 2: rendered profile page was sitting one hop away.
- 20:18Speaker 1: You add follower redirect back into the code. You run
- 20:21Speaker 1: the test suite one last time. Registration, log in, profile,
- 20:25Speaker 1: view admin check data update, green lights across the board.
- 20:29Speaker 1: The test passes.
- 20:30Speaker 2: It's immensely satisfying. But you know, if you step back
- 20:33Speaker 2: and look at the script you just wrote, you realize
- 20:35Speaker 2: you have actually broken a fundamental rule taught on day
- 20:39Speaker 2: one of programming wait, which is that functions should be
- 20:42Speaker 2: small and conventional wisdom says a function should do exactly
- 20:45Speaker 2: one thing, do it cleanly, and be as concise as possible.
- 20:48Speaker 1: Oh right, But this integration test is massive. It's a sprawling,
- 20:52Speaker 1: fifty line monster of a function.
- 20:54Speaker 2: And it has to be You want to cram as
- 20:57Speaker 2: much sequential activity into an integration test as huge humanly possible,
- 21:01Speaker 2: because a real user doesn't use an app in clean, isolated,
- 21:04Speaker 2: single purpose functions.
- 21:05Speaker 1: No they don't. They use it continuously exactly.
- 21:07Speaker 2: They click the wrong things, they navigate backward, they submit data,
- 21:10Speaker 2: they bounce around. The longer and more complex your integration
- 21:13Speaker 2: test is, the more thoroughly you are simulating the messy
- 21:17Speaker 2: reality of human behavior.
- 21:19Speaker 1: It is the ultimate safety net for your architecture. But
- 21:22Speaker 1: we do have to acknowledge the wall we hit earlier,
- 21:25Speaker 1: the viewjs JavaScript illusion. Integration tests are phenomenal for back
- 21:29Speaker 1: end routing, database logic, and HTML structure, but they are
- 21:33Speaker 1: entirely blind to complex UI rendering.
- 21:36Speaker 2: Yeah, if your dating app requires a user to physically
- 21:39Speaker 2: click and drag to swipe left on a dynamically rendered
- 21:42Speaker 2: JavaScript card, an integration test simply cannot verify that that
- 21:46Speaker 2: swipe animation works.
- 21:47Speaker 1: To test actual browser behavior, you need an actual browser.
- 21:51Speaker 2: That is the realm of system tests or end to
- 21:53Speaker 2: end tests. Tools that literally spin up a hidden instance
- 21:56Speaker 2: of a real browser, load the JavaScript engine, and click
- 21:59Speaker 2: the button with an automated ghost mouse.
- 22:01Speaker 1: But that is a complex, deep dive for another day.
- 22:04Speaker 1: You have survived the murky diagnostic waters of integration testing.
- 22:08Speaker 1: But before you go, I want to leave you with
- 22:09Speaker 1: a thought experiment to chew on.
- 22:11Speaker 2: Ooh, where does this technology go next?
- 22:13Speaker 1: Exactly? Think about the massive, sprawling user flow test we
- 22:17Speaker 1: just built by hand. It took us hours of scripting
- 22:20Speaker 1: and debugging to guess how a user might navigate our app.
- 22:24Speaker 1: But what if we didn't have to guess. We are
- 22:27Speaker 1: entering an era where AI can ingest the actual telemetry
- 22:30Speaker 1: data of how human beings use an application in production.
- 22:33Speaker 2: That's wild. The machine learning models could see that, say,
- 22:37Speaker 2: eighty percent of users register, immediately click the settings gear,
- 22:40Speaker 2: fail to upload a photo, and then try to swipe.
- 22:43Speaker 1: Exactly if an AI can map that messy real world
- 22:47Speaker 1: human behavior, how long until the AI just writes these
- 22:50Speaker 1: massive integration tests for us automatically. Well, the developer of
- 22:54Speaker 1: the future even need to know what a CSS selector is,
- 22:56Speaker 1: so we'll they just ask the testing engine to act
- 22:58Speaker 1: like a confused teenager on a frime night and let
- 23:01Speaker 1: the software figure out how to break the app. Something
- 23:03Speaker 1: for you to ponder the next time you're staring at
- 23:05Speaker 1: a failed test console. Oh and one last thing to
- 23:08Speaker 1: test your own memory well pop quiz stick quick one.
- 23:11Speaker 1: Think back on our deep dive today. If you run
- 23:14Speaker 1: a test and it completely crashes because a variable is missing,
- 23:17Speaker 1: did you encounter a test failure or a test error?
- 23:20Speaker 1: And more importantly, which file do you actually need to fix?
- 23:24Speaker 1: Think about it until next time, keep questioning the code