Testing
Because if I have to write one more curl statement, I'm going to lose it.
It's important for any API to have a set of automated tests, that way you can change things with at least a little bit of confidence that the behavior will stay the same at the output. Thruster comes will a full test suite to help developer test their code. In order to use it, we'll have to do a little refactoring.
First, we're going to make a method specifically to create an app
. Right now we're building the app and the server in the main
function, so we'll split that out. First create a new file, app.rs
We basically copied over everything from main.rs
and made Ctx
, ServerConfig
, and RequestConfig
public. With that, we need to update our main.rs
file as well, it'll get a lot simpler without all the app creation and middleware functions:
In order to use our library in tests we also need, well, a library! So create a lib.rs
file. In that file, add a single line (for now.)
Now we're in a great place to write a simple test, starting with our /hello
route. Make a new file in a new folder named tests/
called hello.rs
. I like to name tests based on routes, but you can really organize them whoever you please.
In hello.rs
, we're going to make a very simple test as follows:
Some of this code should look very familiar! We're making a new database pool, and then running our quick schema to make sure it's in the correct state. One difference to note is that we're explicitly calling out port 5433 instead of the typical 5432. This is to make sure that we're not running tests on our local development database! We should start another container for testing before we actually run this,
In the above rust code, I'll note a few important pieces here:
Note that we're creating the app the same way we would in the
main
function, however we're also running this littlecommit
method at the end. It's not terribly important for this tutorial as to why that needs to be run, but loosely it's a method that gets run when the server starts so that it can efficiently serve routes. If you didn't run it you would get 404 status codes. If you feel like it, go ahead and comment it out and see what part of the test fails.Thruster provides the
Testable
trait, which basically is an extension ofApp
specifically for testing. It uses the same names as setting a new route for the app, so we use the "fully qualified syntax" so that the compiler knows we want to call the test version. This test version basically fakes a request using the same router and middleware, but ignoring the "Server" backend piece (the piece that thruster can easily swap out between hyper, actix, or the homegrown version for example.)
Now you should be ready to run the test! Run
and bask in the glory of your newly tested code.
Last updated