unit testing examples in the SignalR 2 documentation을 사용해 보았습니다. 그러나 이제는 허브가 특정 상황에서만 단일 사용자에게 알리는 지 테스트하고 싶습니다.SignalR 2 허브를 테스트하는 방법은 단일 사용자에게만 통보합니까?
내 허브 코드는 다음과 같습니다
public class NotificationsHub : Hub
{
public void RaiseAlert(string message)
{
Clients.All.RaiseAlert(message);
}
public void RaiseNotificationAlert(string userId)
{
if (userId == null)
{
// Notify all clients
Clients.All.RaiseAlert("");
return;
}
// Notify only the client for this userId
Clients.User(userId).RaiseAlert("");
}
}
(그것이 마이크로 소프트의 예를 기반으로)이 같은 모든 클라이언트가 통지되는 모습을 확인하는 내 단위 테스트 : 나는 '무엇
[Test]
public void NotifiesAllUsersWhenNoUserIdSpecified()
{
// Based on: https://docs.microsoft.com/vi-vn/aspnet/signalr/overview/testing-and-debugging/unit-testing-signalr-applications
// Arrange
// This is faking the
var mockClients = new Mock<IClientContract>();
mockClients.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable();
// A mock of our SignalR hub's clients
var mockClientConnCtx = new Mock<IHubCallerConnectionContext<dynamic>>();
mockClientConnCtx.Setup(m => m.All).Returns(mockClients.Object);
// Set the hub's connection context to the mock context
var hub = new NotificationsHub
{
Clients = mockClientConnCtx.Object
};
// Action
hub.RaiseNotificationAlert(null);
// Assert
mockClients.Verify(m => m.RaiseAlert(It.IsAny<string>()));
}
확실하지 않다면 var mockClients = new Mock<IClientContract>()
라인으로 표시된 클라이언트 컬렉션을 개별 클라이언트 그룹으로 변경하여 사용자 1에게 통지하면 사용자 2와 3에게 통지되지 않았 음을 테스트 할 수 있습니까?