@Test("then it should return a status 200 (OK) message") func thenItShouldReturnAStatus20OKMessage() async throws {
try await withApp { app in
try await app.testing().test(.GET, "v1/rovers") { res async in
#expect(res.status == .ok)
}
}
}
This test consistently fails with a 404 (not found) error. Though the logs shows indeed the /v1/rovers endpoint is called.
However, when I run the server and call the endpoint directly using curl http://localhost:8080/v1/rovers/ I do get the expected result back:
I haven't checked the project but (regarding the errors in the image) off-hand I'd guess there is either a missing import or a missing dependency declaration in Package.swift. Perhaps "VaporTesting" or "Testing"?
Ok I can see the issue - the problem you're hitting is that the compiler is picking VaporTesting's withApp instead of the one you've defined so it's never registering the routes. You can fix this pretty easily by getting Vapor Testing to call the configure function. If you change your test file to
@Suite("The MarsRover API should") struct MarsRoverAPITests {
@Suite("when the create endpoint is correctly falled") struct CreateEndPointCorrectlyCalled {
@Test("then it should return a status 201 (Created) message") func thenItShouldReturnAStatus201CreatedMessage() async throws {
try await withApp(configure: configure) { app in
try await app.testing().test(.POST, "v1/rovers") { res async in
#expect(res.status == .created)
}
}
}
}
@Suite("when the get all rovers endpoint is correctly called") struct GetAllRoversEndPointCorrectlyCalled {
@Test("then it should return a status 200 (OK) message") func thenItShouldReturnAStatus20OKMessage() async throws {
try await withApp(configure: configure) { app in
try await app.testing().test(.GET, "v1/rovers") { res async in
#expect(res.status == .ok)
}
}
}
}
}
All the tests pass. (Note the withApp(configure: configure) call) you can remove your withApp implementation