Unit Testing SignalR Applications
This article describes using the Unit Testing features of SignalR 2.
Software versions used in this topic
- Visual Studio 2013
- .NET 4.5
- SignalR version 2
Questions and comments
Please leave feedback on how you liked this tutorial and what we could improve in the comments at the bottom of the page. If you have questions that are not directly related to the tutorial, you can post them to the ASP.NET SignalR forum or StackOverflow.com.
## Unit testing SignalR applications
You can use the unit test features in SignalR 2 to create unit tests for your SignalR application. SignalR 2 includes the IHubCallerConnectionContext interface, which can be used to create a mock object to simulate your hub methods for testing.
In this section, you’ll add unit tests for the application created in the Getting Started tutorial using XUnit.net and Moq.
XUnit.net will be used to control the test; Moq will be used to create a mock object for testing. Other mocking frameworks can be used if desired; NSubstitute is also a good choice. This tutorial demonstrates how to set up the mock object in two ways: First, using a dynamic
object (introduced in .NET Framework 4), and second, using an interface.
Contents
This tutorial contains the following sections.
In this section, you’ll add a unit test for the application created in the Getting Started tutorial using a dynamic object.
- Install the XUnit Runner extension for Visual Studio 2013.
- Either complete the Getting Started tutorial, or download the completed application from MSDN Code Gallery.
If you are using the download version of the Getting Started application, open Package Manager Console and click Restore to add the SignalR package to the project.
Add a project to the solution for the unit test. Right-click your solution in Solution Explorer and select Add, New Project…. Under the C# node, select the Windows node. Select Class Library. Name the new project TestLibrary and click OK.
Add a reference in the test library project to the SignalRChat project. Right-click the TestLibrary project and select Add, Reference…. Select the Projects node under the Solution node, and check SignalRChat. Click OK.
Add the SignalR, Moq, and XUnit packages to the TestLibrary project. In the Package Manager Console, set the Default Project dropdown to TestLibrary. Run the following commands in the console window:
Install-Package Microsoft.AspNet.SignalR
Install-Package Moq
Install-Package XUnit
- Create the test file. Right-click the TestLibrary project and click Add…, Class. Name the new class Tests.cs.
Replace the contents of Tests.cs with the following code.
[!code-csharpMain]
1: using System;
2: using Xunit;
3: using SignalRChat;
4: using Microsoft.AspNet.SignalR.Hubs;
5: using Moq;
6: using System.Dynamic;
7:
8: namespace TestLibrary
9: {
10: public class Tests
11: {
12: [Fact]
13: public void HubsAreMockableViaDynamic()
14: {
15: bool sendCalled = false;
16: var hub = new ChatHub();
17: var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
18: hub.Clients = mockClients.Object;
19: dynamic all = new ExpandoObject();
20: all.broadcastMessage = new Action<string, string>((name, message) => {
21: sendCalled = true;
22: });
23: mockClients.Setup(m => m.All).Returns((ExpandoObject)all);
24: hub.Send("TestUser", "TestMessage");
25: Assert.True(sendCalled);
26: }
27: }
28: }
Mock
object from the Moq library, of type IHubCallerConnectionContext (in SignalR 2.1, assigndynamic
for the type parameter.) TheIHubCallerConnectionContext
interface is the proxy object with which you invoke methods on the client. ThebroadcastMessage
function is then defined for the mock client so that it can be called by theChatHub
class. The test engine then calls theSend
method of theChatHub
class, which in turn calls the mockedbroadcastMessage
function.- Build the solution by pressing F6.
Run the unit test. In Visual Studio, select Test, Windows, Test Explorer. In the Test Explorer window, right-click HubsAreMockableViaDynamic and select Run Selected Tests.
Verify that the test passed by checking the lower pane in the Test Explorer window. The window will show that the test passed.
In this section, you’ll add a test for the application created in the Getting Started tutorial using an interface that contains the method to be tested.
- Complete steps 1-7 in the Unit testing with Dynamic tutorial above.
Replace the contents of Tests.cs with the following code.
[!code-csharpMain]
1: using Xunit;
2: using SignalRChat;
3: using Microsoft.AspNet.SignalR.Hubs;
4: using Moq;
5:
6: namespace TestLibrary
7: {
8: public class Tests
9: {
10:
11: public interface IClientContract
12: {
13: void broadcastMessage(string name, string message);
14: }
15: [Fact]
16: public void HubsAreMockableViaType()
17: {
18: var hub = new ChatHub();
19: var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
20: var all = new Mock<IClientContract>();
21: hub.Clients = mockClients.Object;
22: all.Setup(m => m.broadcastMessage(It.IsAny<string>(),
23: It.IsAny<string>())).Verifiable();
24: mockClients.Setup(m => m.All).Returns(all.Object);
25: hub.Send("TestUser", "TestMessage");
26: all.VerifyAll();
27: }
28: }
29: }
In the code above, an interface is created defining the signature of the
The test then creates an instance ofbroadcastMessage
method for which the test engine will create a mock client. A mock client is then created using theMock
object, of type IHubCallerConnectionContext (in SignalR 2.1, assigndynamic
for the type parameter.) TheIHubCallerConnectionContext
interface is the proxy object with which you invoke methods on the client.ChatHub
, and then creates a mock version of thebroadcastMessage
method, which in turn is invoked by calling theSend
method on the hub.- Build the solution by pressing F6.
Run the unit test. In Visual Studio, select Test, Windows, Test Explorer. In the Test Explorer window, right-click HubsAreMockableViaDynamic and select Run Selected Tests.
Verify that the test passed by checking the lower pane in the Test Explorer window. The window will show that the test passed.
|