Modern Concurrency in Swift by By Marin Todorov
Author:By Marin Todorov
Language: eng
Format: epub
Publisher: Ray Wenderlich
Adding a simple asynchronous test
A critical point to remember when adding asynchronous tests is to add the async keyword to each test method. Doing this lets you await your code under test and easily verify the output.
Add the following method to BlabberTests to create your first test:
func testModelSay() async throws { try await model.say("Hello!") }
Since the model is already configured to use the test-suitable URL session, you donât need to do any additional setup â you just call say(_:) right away.
At this point, youâre ready to add your test expectations. First, youâll verify that the last request the network performed, model.say("Hello!"), was sent to the correct URL.
Add the following code to do that:
let request = try XCTUnwrap(TestURLProtocol.lastRequest) XCTAssertEqual( request.url?.absoluteString, "http://localhost:8080/chat/say" )
You first unwrap the optional TestURLProtocol.lastRequest, then check that the URL matches the expected address: http://localhost:8080/chat/say.
Now that youâve verified that the model sends the data to the correct endpoint, you can check that it also sends the correct data.
Finish up your test with the following piece of code:
let httpBody = try XCTUnwrap(request.httpBody) let message = try XCTUnwrap(try? JSONDecoder() .decode(Message.self, from: httpBody)) XCTAssertEqual(message.message, "Hello!")
You expect request.httpBody to decode as a Message. Once decoded, you assert that the message text equals âHello!â, as expected.
If you wrote asynchronous tests prior to Swift 5.5, youâre likely excited about the brevity and clarity of this test code. And if you havenât written asynchronous tests before, you really donât need to know the lengths you had to go to set up a good asynchronous test back then!
To run the test, click Play in the editor gutter, to the left of func testModelSay()..., or press Command-U to run all tests.
Regardless of how you go about it, youâll see the test pass and a green check mark (the best check mark!) will appear next to the test name in Xcode:
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Exploring Deepfakes by Bryan Lyon and Matt Tora(7760)
Robo-Advisor with Python by Aki Ranin(7657)
Offensive Shellcode from Scratch by Rishalin Pillay(6120)
Microsoft 365 and SharePoint Online Cookbook by Gaurav Mahajan Sudeep Ghatak Nate Chamberlain Scott Brewster(5061)
Ego Is the Enemy by Ryan Holiday(4960)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4440)
Python for ArcGIS Pro by Silas Toms Bill Parker(4198)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(3904)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3643)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(3286)
Speed Up Your Python with Rust by Maxwell Flitton(3233)
Liar's Poker by Michael Lewis(3228)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(3195)
Extreme DAX by Michiel Rozema & Henk Vlootman(3176)
Agile Security Operations by Hinne Hettema(3125)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(3110)
Essential Cryptography for JavaScript Developers by Alessandro Segala(3083)
Cryptography Algorithms by Massimo Bertaccini(3002)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2985)
