2017-10-26 2 views
1

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에게 통지되지 않았 음을 테스트 할 수 있습니까?

답변

0

단위 테스트 그룹에 대한 또 다른 질문이 있으며 one of the answersunit tests for the SignalR codebase을 가리 킵니다.

이러한 예제를 보면 User 메서드에 대한 호출 조롱을 mockClients에 추가해야했습니다. 결국 이렇게 보였습니다.

public interface IClientContract 
{ 
    void RaiseAlert(string message); 
} 

[Test] 
public void NotifiesOnlySpecifiedUserWhenUserIdSent() 
{ 
    // Adapted from code here: https://github.com/SignalR/SignalR/blob/dev/tests/Microsoft.AspNet.SignalR.Tests/Server/Hubs/HubFacts.cs 

    // Arrange 

    // Set up the individual mock clients 
    var client1 = new Mock<IClientContract>(); 
    var client2 = new Mock<IClientContract>(); 
    var client3 = new Mock<IClientContract>(); 

    client1.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable(); 
    client2.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable(); 
    client3.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable(); 

    // set the Connection Context to return the mock clients 
    var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>(); 
    mockClients.Setup(m => m.User("1")).Returns(client1.Object); 
    mockClients.Setup(m => m.User("2")).Returns(client2.Object); 
    mockClients.Setup(m => m.User("3")).Returns(client3.Object); 

    // Assign our mock context to our hub 
    var hub = new NotificationsHub 
    { 
     Clients = mockClients.Object 
    }; 

    // Act 
    hub.RaiseNotificationAlert("2"); 

    // Assert 
    client1.Verify(m => m.RaiseAlert(It.IsAny<string>()), Times.Never); 
    client2.Verify(m=>m.RaiseAlert(""), Times.Once); 
    client3.Verify(m => m.RaiseAlert(It.IsAny<string>()), Times.Never); 
} 
관련 문제