Question

I'm working on building up Unit Tests for our SignalR 2.x implementation. Our implementation utilizes accessing request cookies stored in the Context.

So, to build out our unit tests, we have to create a mocked cookie collection and associate it with the mocked request object.

I've seen the following code block that does this in SignalR 1.x:

const string connectionId = "1234";
const string hubName = "Chat"; 
var mockConnection = new Mock<IConnection>();
var mockUser = new Mock<IPrincipal>();
var mockCookies = new Mock<IRequestCookieCollection>();
var mockPipelineInvoker = new Mock<IHubPipelineInvoker>();

var mockRequest = new Mock<IRequest>();
mockRequest.Setup(r => r.User).Returns(mockUser.Object);
mockRequest.Setup(r => r.Cookies).Returns(mockCookies.Object);

StateChangeTracker tracker = new StateChangeTracker();
Clients = new HubConnectionContext(mockPipelineInvoker.Object, mockConnection.Object, hubName, connectionId, tracker);
Context = new HubCallerContext(mockRequest.Object, connectionId);

I'm running into issues trying to create the mocked cookie collection. IRequestCookieCollection above is undefined.

var mockCookies = new Mock<IRequestCookieCollection>();

Did this move somewhere else in the SignalR libraries? Or.., is there a different way to do this??

Thanks, JohnB

Was it helpful?

Solution

In SignalR 1.0, the type of IRequest.Cookies was changed from IRequestCookieCollection to IDictionary<string, Cookie>. As part of this change, IRequestCookieCollection was removed.

Changing mockCookies to reflect to the new type should fix your issue:

var mockCookies = new Mock<IDictionary<string, Cookie>>();

However, it might be painful mocking out an IDictionary, so it is probably easier to use a normal Dictionary instead:

var cookies = new Dictionary<string, Cookie>();
// ...
mockRequest.Setup(r => r.Cookies).Returns(cookies);

https://github.com/SignalR/SignalR/issues/1034

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top