Skip to content

Commit

Permalink
fix: 修正 Fail Test 玩家建立連線失敗因為遊戲不存在
Browse files Browse the repository at this point in the history
改為驗證
  • Loading branch information
aa89227 committed Aug 13, 2023
1 parent c753b33 commit 991076e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 29 deletions.
11 changes: 2 additions & 9 deletions Server/Hubs/MonopolyHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,15 @@ public override Task OnConnectedAsync()
var gameIdStringValues = httpContext.Request.Query["gameid"];
if (gameIdStringValues.Count == 0)
{
Clients.Caller.PlayerJoinGameFailedEvent("Not pass game id");
Context.Abort();
throw new GameNotFoundException($"Not pass game id");
}
string gameId = gameIdStringValues.ToString();
string userId = Context.User!.FindFirst(x => x.Type == ClaimTypes.Sid)!.Value;
if (!_repository.IsExist(gameId))
{
Clients.Caller.PlayerJoinGameFailedEvent($"Can not find the game that id is {gameId}");
Context.Abort();
throw new GameNotFoundException($"Can not find the game that id is {gameId}");
}
var game = _repository.FindGameById(gameId);
if (!game.Players.Any(p => p.Id == userId))
{
Clients.Caller.PlayerJoinGameFailedEvent($"Can not find the player whose id is {userId}");
Context.Abort();
}
Groups.AddToGroupAsync(Context.ConnectionId, gameId);
Clients.Group(gameId).PlayerJoinGameEvent(userId!);
return base.OnConnectedAsync();
Expand Down
20 changes: 1 addition & 19 deletions Test/ServerTests/AcceptanceTests/PlayerJoinGameTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,7 @@ public async Task 玩家建立連線失敗因為遊戲不存在()
VerificationHub hub = await server.CreateHubConnectionAsync("2", "A");

// Assert
hub.Verify<string>(nameof(IMonopolyResponses.PlayerJoinGameFailedEvent), _ => true);
}

[TestMethod]
[Description("""
Given: Id為1的遊戲,裡面有玩家 A B C
When: 玩家D建立連線到Id為1的房間
Then: 玩家D建立連線失敗
""")]
public async Task 玩家建立連線失敗因為此玩家不在遊戲中()
{
// Arrange
await CreateGameAsync("A", "A", "B", "C");

// Act
VerificationHub hub = await server.CreateHubConnectionAsync("1", "D");

// Assert
hub.Verify<string>(nameof(IMonopolyResponses.PlayerJoinGameFailedEvent), _ => true);
hub.VerifyDisconnection();
}
/// <summary>
/// call API POST "/" Body: <paramref name="playerIds"/>.
Expand Down
6 changes: 5 additions & 1 deletion Test/ServerTests/MonopolyTestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ public VerificationHub(HubConnection Connection)
Queues = new();
ListenAllEvent();
}

public async void VerifyDisconnection(int delay = 1000)
{
await Task.Delay(delay);
Assert.AreEqual(HubConnectionState.Disconnected, Connection.State);
}
// 利用反射讀出所有HubResponse的Method,並且設置On function
private void ListenAllEvent()
{
Expand Down

0 comments on commit 991076e

Please sign in to comment.