-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatHubTests.cs
65 lines (55 loc) · 2.02 KB
/
ChatHubTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Net.Http;
using System.Threading.Tasks;
using ChatHub.IntegrationTests.Common;
using FluentAssertions;
using Microsoft.AspNetCore.SignalR.Client;
using Xunit;
namespace ChatHub.IntegrationTests
{
public class ChatHubTests : IClassFixture<WebApiFactory>
{
private readonly WebApiFactory _factory;
public ChatHubTests(WebApiFactory factory)
{
_factory = factory;
}
[Fact]
public async Task sendMessage_should_send_the_user_and_message_to_all_clients()
{
// Arrange
_factory.CreateClient(); // need to create a client for the server property to be available
var server = _factory.Server;
var connection = await StartConnectionAsync(server.CreateHandler(), "chat");
connection.Closed += async error =>
{
await Task.Delay(new Random().Next(0, 5) * 1000);
await connection.StartAsync();
};
// Act
string user = null;
string message = null;
connection.On<string,string>("OnReceiveMessage", (u, m) =>
{
user = u;
message = m;
});
//await connection.InvokeCoreAsync("SendMessage", new object[] { "super_user", "Hello World!!" });
await connection.InvokeAsync("SendMessage", "super_user", "Hello World!!");
//Assert
user.Should().Be("super_user");
message.Should().Be("Hello World!!");
}
private static async Task<HubConnection> StartConnectionAsync(HttpMessageHandler handler, string hubName)
{
var hubConnection = new HubConnectionBuilder()
.WithUrl($"ws://localhost/hubs/{hubName}", o =>
{
o.HttpMessageHandlerFactory = _ => handler;
})
.Build();
await hubConnection.StartAsync();
return hubConnection;
}
}
}