Aspnetcore: Need example on stubbing client HubConnection

Created on 3 Mar 2019  路  5Comments  路  Source: dotnet/aspnetcore

Describe the bug

None core version had a interface IHubProxy which we could Mock easily, the new client has a none abstract concrete typ called HubConnection, I've spent the last two hours trying to mock all its dependecnies so we can Unit test. But without success, can you point me to en example how to mock the HubConnection? Thanks

area-signalr

Most helpful comment

Using HubConnection it is really horrible to test. Wrapping it in a class is not ideal as it is preferable to verify actual calls to HubConnection. It would be easier to test if it had an interface, then you could mock the 'Core' methods, but you're mostly using all these static extension methods which could be overloading methods but aren't. I really wonder what the design decisions for this were.

All 5 comments

It's not a mockable type. I'd suggest wrapping it in your own interface and mocking that.

Using HubConnection it is really horrible to test. Wrapping it in a class is not ideal as it is preferable to verify actual calls to HubConnection. It would be easier to test if it had an interface, then you could mock the 'Core' methods, but you're mostly using all these static extension methods which could be overloading methods but aren't. I really wonder what the design decisions for this were.

Like @davidfowl articulated above, the HubConnection isn't really mockable, nor did we really intend on it being mockable. My feeling on this is that it's an functional detail, and thought unfortunate it prevents 100% test coverage, I'd argue that when I've wanted to test scenarios using a hub, my interest was in validating that "an event fired" when I'd expect it to fire more often than not. In those cases i'd wrap it in a class, or even abstract it into an interface as @davidfowl proposed, and use eventing tests.

I am interested in other scenarios here and ideas, but I don't foresee us changing this implementation.

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!

Was this page helpful?
0 / 5 - 0 ratings