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