I Would like to test my Hub in SignalR, what is the best approach?
Possible solutions I have thought about so far:
- Create a testable Hub
- Abstract logic to separate class
- Selenium (would like to test smaller units)
- Or is it some SignalR testing features have overlooked
Currently using SignalR 0.4, and NUnit as the testing framework.
This is modified version of Iarsm's answer, to work with XUnit and MOQ.
}
I personally think best way is as given - http://www.asp.net/signalr/overview/testing-and-debugging/unit-testing-signalr-applications
This link 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:
Then the site shows that you can use this testable hub to write unit tests:
With the SignalR 2.0 you can do it this way:
This question is from a while ago, but I'll do my best to answer anyway.
If you have a lot of logic in your actual hub class, it would certainly make sense to abstract the logic to a separate class. I did the same for my SignalR-powered multiplayer demo. The only behaviour that should go in your hub class itself is the one related to messaging. All further action should be delegated.
Note: This is very much like the guidelines for controller design in ASP .NET MVC: Keep your controllers small and delegate the real work.
If you want integration tests with SignalR actually doing some work, selenium webdriver would be a good option. But you will probably need to do some tweaking to get the SignalR messaging working perfectly in the context of the tests. Do a google search for "signalr selenium" (without the quotes) to get started on the right track.
Some blogposts about automated tests for SignalR => here and here
It's quite simple to create to unit test SignalR hubs using a couple of neat tricks. One thing to note is that SignalR uses
dynamic
classes which might not be supported by your mocking framework (I use NSubstitute).Rewriting this to use e.g. Moq should be pretty simple.