Dynamic return type and SignalR

736 views
Skip to first unread message

Aaron Powell

unread,
Jan 28, 2015, 5:17:15 AM1/28/15
to nsubs...@googlegroups.com
I'm working on some unit tests for a SingalR hub which needs me to mock a method that returns a dynamic.

The Hub setup in my test looks like this:

var hub = new EnterpriseHub(

collection

);

var clients = Substitute.For<IHubCallerConnectionContext<dynamic>>();

dynamic all = new ExpandoObject();

all.LogActivity = new Action<EventMessage>(x =>

{

//noop

});

clients.Group(Arg.Any<string>())

.Returns((ExpandoObject)all);

hub.Clients = clients;


But it results in this error:
Test Name: Should_save_SiteId_when_source_was_site
Test FullName: MyProject.Tests.Hubs.When_sending_event_to_EnterpriseHub.Should_save_SiteId_when_source_was_site
Test Source: C:\_Projects\MyProject\MyProject.Tests\Hubs\When_sending_event_to_EnterpriseHub.cs : line 18
Test Outcome: Failed
Test Duration: 0:00:00.383
Result Message: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : Cannot perform runtime binding on a null reference
Result StackTrace: 
at CallSite.Target(Closure , CallSite , Object , ExpandoObject )
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)
   at Weir.MDP.Enterprise.Tests.Hubs.When_sending_event_to_EnterpriseHub.Should_save_SiteId_when_source_was_site() in C:\_Projects\MyProject\MyProject.Tests\Hubs\When_sending_event_to_EnterpriseHub.cs:line 36

I'm using NSub 1.8.0.0 and I've seen there were problems with mocking methods returning dynamic (which client.Group does) but I was under the impression that was solved in 1.7.something.

Anyone got any thoughts?

Aaron Powell

unread,
Jan 28, 2015, 6:06:16 AM1/28/15
to nsubs...@googlegroups.com

So it turns out I’m probably shooting myself in the foot with my approach, my mock doesn’t need to be a generic type of `dynamic`, it can be anything, it’s just SignalR that (later) exposes it as `dynamic`.

 

I changed this:

var clients = Substitute.For<IHubCallerConnectionContext<dynamic>>();

To this:

var clients = Substitute.For<IHubCallerConnectionContext<ExpandoObject>>();

And it works fine.

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at http://groups.google.com/group/nsubstitute.
For more options, visit https://groups.google.com/d/optout.

David Tchepak

unread,
Jan 28, 2015, 6:25:41 AM1/28/15
to nsubs...@googlegroups.com
Hi Aaron,
Alexandr has a great explanation of the issue in the comments of this PR: https://github.com/nsubstitute/NSubstitute/pull/141
Have you tried using the extension method directly instead?

  Substitute.Returns(clients.Group(Arg.Any<string>()), all);

There's a more complete example on SO that also involves signalr: http://stackoverflow.com/a/16008194/906

Hope this helps.
Regards,
David



Reply all
Reply to author
Forward
0 new messages