Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This <a href="http://3amcode.blogspot.com/2012/07/unit-testing-signalr-hubs.html" rel="nofollow noreferrer">link</a> shows how to unit test SignalR hub methods using Moq. You mock up the respository, clients, context, and the caller. Here's the code from the site, I made some minor changes to make it work with the latest SignalR:</p> <pre class="lang-cs prettyprint-override"><code>public class TestableChatHub : ChatHub { public Mock&lt;IChatRepository&gt; MockChatRepository { get; private set; } public TestableChatHub(Mock&lt;IChatRepository&gt; mockChatRepository) : base(mockChatRepository.Object) { const string connectionId = "1234"; const string hubName = "Chat"; var mockConnection = new Mock&lt;IConnection&gt;(); var mockUser = new Mock&lt;IPrincipal&gt;(); var mockCookies = new Mock&lt;IRequestCookieCollection&gt;(); var mockRequest = new Mock&lt;IRequest&gt;(); mockRequest.Setup(r =&gt; r.User).Returns(mockUser.Object); mockRequest.Setup(r =&gt; r.Cookies).Returns(mockCookies.Object); Clients = new ClientProxy(mockConnection.Object, hubName); Context = new HubCallerContext(mockRequest.Object, connectionId); var trackingDictionary = new TrackingDictionary(); Caller = new StatefulSignalProxy( mockConnection.Object, connectionId, hubName, trackingDictionary); } } </code></pre> <p>Then the site shows that you can use this testable hub to write unit tests:</p> <pre class="lang-cs prettyprint-override"><code> [TestClass] public class ChatHubTests { private TestableChatHub _hub; public void SetUpTests() { _hub = GetTestableChatHub(); } [Test] public void ExampleTest() { SetUpTests(); const string message = "test"; const string connectionId = "1234"; var result = _hub.Send(message); _hub.MockChatRepository.Verify(r =&gt; r.SaveMessage(message, connectionId)); Assert.IsTrue(result); } private TestableChatHub GetTestableChatHub() { var mockRepository = new Mock&lt;IChatRepository&gt;(); mockRepository.Setup(m =&gt; m.SaveMessage( It.IsAny&lt;string&gt;(), It.IsAny&lt;string())).Returns(true); return new TestableChatHub(mockRepository); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload