You want to write test cases for every route right? Yes, I want too. But today, I met a question - How to write test cases to test routes that render model to Leaf page?
We all know testing html is complicated, I want to test the model which will be rendered to the html page, but how?
Let’s see the answer.
First, we need to write our ViewRender
, we want to use the custom ViewRender to get the model before it is rendered to html page.
1 | import Vapor |
You see, we get the rendering destination templatePath
, and the context capturedContext
. Maybe you don’t know what context is, just see the route hander, you will undertand.
1 | func getArticlesHandler(req: Request) throws -> EventLoopFuture<View> { |
The HomeContext is the Context the Leaf framework needs, it wraps our model.
Second, we need to register the ViewRender to our app.
1 | final class ArticleRoutesTests: XCTestCase { |
Third, write the test case to compare the route path and model.
1 | func testGetArticles() throws { |
Done! Enjoy testing.
Thanks to @0xTim for discussion about it.