Mocking SignalR hub object

1,477 views
Skip to first unread message

Patrick Fletcher

unread,
Jan 6, 2014, 5:49:16 PM1/6/14
to moq...@googlegroups.com
I'm trying to create a unit test for a SignalR application (the one created in the Getting Started tutorial hereusing Moq and XUnit. 

Some of the hub's functionality- the broadcastMessage method- is defined in the Javascript client. How do I set up the mock client object? Here's what I've got so far:


using System;
using Xunit;
using SignalRChat;
using Microsoft.AspNet.SignalR.Hubs;
using Moq;
using System.Dynamic;


namespace TestLibrary
{
    public class Tests
    {
        [Fact]
        public void HubsAreMockableViaDynamic()
        {
            bool sendCalled = false;
            var hub = new ChatHub();
            var mockClients = new Mock<IHubCallerConnectionContext>();
            hub.Clients = mockClients.Object;
            dynamic all = new ExpandoObject();
            all.send = new Action<string>(message =>
            {
                sendCalled = true;
            });
            mockClients.Setup(m => m.All).Returns((ExpandoObject)all);
            hub.Send("TestUser", "TestMessage");
            Assert.True(sendCalled);
        }
    }
}
Here's the result:
C:\Projects\SignalRChat\TestLibrary\bin\Debug>xunit.console.clr4 TestLibrary.dll

xUnit.net console test runner (64-bit .NET 4.0.30319.34003)
Copyright (C) 2013 Outercurve Foundation.

xunit.dll:     Version 1.9.2.1705
Test assembly: C:\Projects\SignalRChat\TestLibrary\bin\Debug\TestLibrary.dll

TestLibrary.Tests.HubsAreMockableViaDynamic [FAIL]
   Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'System.Dynamic.Expan
doObject' does not contain a definition for 'broadcastMessage'
   Stack Trace:
      at CallSite.Target(Closure , CallSite , Object , String , String )
      at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite
 site, T0 arg0, T1 arg1, T2 arg2)
      c:\Projects\SignalRChat\SignalRChat\ChatHub.cs(11,0): at SignalRChat.ChatH
ub.Send(String name, String message)
      c:\Projects\SignalRChat\TestLibrary\Tests.cs(31,0): at TestLibrary.Tests.H
ubsAreMockableViaDynamic()

1 total, 1 failed, 0 skipped, took 1.371 seconds

Daniel Cazzulino

unread,
Jan 6, 2014, 5:55:39 PM1/6/14
to moqdisc
Maybe a good place to start would be SignalR tests themselves? They use Moq too... 



/kzu

--
Daniel Cazzulino


--
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Moq Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to moqdisc+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages