Re: [nsubstitute] Raise Event not working

1,170 views
Skip to first unread message

Anthony Egerton

unread,
Apr 26, 2013, 8:15:55 AM4/26/13
to nsubs...@googlegroups.com
The PostAuthenticateRequest event has to be virtual for NSubstitute to substitute for it.

Cheers,
Anthony

On Fri, Apr 26, 2013 at 5:49 PM, <ted...@gmail.com> wrote:
I am trying to write a unit test to verify the subscription to PostAuthenticateRequest event in the HttpApplication class using NSubstitute.  Here is what I have:ion
 
var flag = false;
var context = Substitute.For<System.Web.HttpApplication>();
context.PostAuthenticateRequest += (sender, args) => flag = true;
context.PostAuthenticateRequest += Raise.Event();
Assert.IsTrue(flag);
 
The test failed.  The Raise.Event() didn't trigger the delegate.  I tried Raise.EventWith(this, new EventArgs()) and Raise.EventWith(new object(), new EventArgs()) to no avail.
 
What did I do wrong?
 
 

 

 

 

 

 
 

 

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


ted...@gmail.com

unread,
Apr 26, 2013, 12:11:06 PM4/26/13
to nsubs...@googlegroups.com, ant...@delfish.com
Thanks for the quick reply.  So how can I use NSubstitute to mock the subscription to this event?

David Tchepak

unread,
Apr 28, 2013, 8:14:26 PM4/28/13
to nsubs...@googlegroups.com
Hi tedmkuo,

Thanks for the quick reply.  So how can I use NSubstitute to mock the subscription to this event? 

Short answer is you can't. :-\

There are a few different ways to handle testing when there are mock-unfriendly classes involved. Can you post a specific example you want to test and we'll see what we can come up with?

Regards,
David

Jordan

unread,
Oct 28, 2013, 10:47:23 AM10/28/13
to nsubs...@googlegroups.com
I'm trying to do the same thing for the Activated EventHandler in the Caliburn.Micro framework related to the Screen class (http://caliburnmicro.codeplex.com/SourceControl/latest#src/Caliburn.Micro/Screen.cs). Since I am referencing the source code directly, I tried making the event virtual, but this made no difference. I've tried:

            var activatedWasCalled = false;

            this._vm.Activated += (sender, args) => activatedWasCalled = true;

            this._vm.Activated += Raise.EventWith( new ActivationEventArgs() );

            this._vm.Activated += Raise.EventWith( new object(), new ActivationEventArgs() );

            Assert.IsTrue( activatedWasCalled );

but activatedWasCalled continues to be false.

Any ideas?

David Tchepak

unread,
Nov 1, 2013, 6:36:14 AM11/1/13
to nsubs...@googlegroups.com
I've raised a bug for this: https://github.com/nsubstitute/NSubstitute/issues/121

For the Screen example there are a few workarounds. The first is to substitute for IScreen instead of Screen. This works:

[Test]
public void Screen()
{
    var vm = Substitute.For<IScreen>();
    var activatedWasCalled = false;
    vm.Activated += (sender, args) => activatedWasCalled = true;
    vm.Activated += Raise.EventWith(new ActivationEventArgs());
    vm.Activated += Raise.EventWith(new object(), new ActivationEventArgs());

    Assert.IsTrue(activatedWasCalled); 
}

Second option which is possible thanks to Caliburn's neat design is to use a real Screen:

[Test]
public void ScreenDirect()
{
    var vm = new Screen();
    var activatedWasCalled = false;
    vm.Activated += (sender, args) => activatedWasCalled = true;

    ((IActivate) vm).Activate();

    Assert.IsTrue(activatedWasCalled); 
}

(I don't think using the real instance will bleed over into other tests, but you'd need to check and confirm)



Reply all
Reply to author
Forward
0 new messages