Game-as-a-Service / Machi-Koro-Cpp

4 stars 1 forks source link

[Test] Make tests independent with each other #9

Open YingChen-Lee opened 1 year ago

YingChen-Lee commented 1 year ago

Drogon test framework is an asynchronous test framework. Therefore, if there are multiple tests, it's possible they run in the same time. In this case, different test cases would not be independent to each other. We need to make the test being executed synchronously.

HuaYuan-Tseng commented 1 year ago

Especially the operation of GameRepository.

DROGON_TEST(GIVEN_empty_WHEN_createGame_THEN_success)
{
    auto client = drogon::HttpClient::newHttpClient(HTTP_ADDRESS);
    std::string gameName1 = "Game1";
    drogon::HttpRequestPtr req1 = getRequestObj("gameName", gameName1, drogon::Post, "/CreateGame/createGame");
    client->sendRequest(req1, [TEST_CTX](drogon::ReqResult res, const drogon::HttpResponsePtr& resp) 
    {
        REQUIRE(res == drogon::ReqResult::Ok);
        REQUIRE(resp != nullptr);
        CHECK(resp->getStatusCode() == 200);
        LOG_INFO << resp->getBody();
    });
    GameRepository::self().ClearAllGames();
}

// TODO(issue #9): Fix the asynchronous problem so that different test cases won't interfere with each other.
DROGON_TEST(GIVEN_gameExists_WHEN_createGameWithSameName_THEN_reject)
{
    auto client = drogon::HttpClient::newHttpClient(HTTP_ADDRESS);
    std::string gameName2 = "Game2";
    drogon::HttpRequestPtr req1 = getRequestObj("gameName", gameName2, drogon::Post, "/CreateGame/createGame");
    client->sendRequest(req1, [TEST_CTX](drogon::ReqResult res, const drogon::HttpResponsePtr& resp) 
    {
        REQUIRE(res == drogon::ReqResult::Ok);
        REQUIRE(resp != nullptr);
    });

    drogon::HttpRequestPtr req2 = getRequestObj("gameName", gameName2, drogon::Post, "/CreateGame/createGame");
    client->sendRequest(req2, [TEST_CTX](drogon::ReqResult res, const drogon::HttpResponsePtr& resp)
    {
        REQUIRE(res == drogon::ReqResult::Ok);
        REQUIRE(resp != nullptr);
        CHECK(resp->getStatusCode() == 400);
        LOG_INFO << resp->getBody();
    });
    GameRepository::self().ClearAllGames();
}
angtsusiong commented 9 months ago

I have a different opinion.