chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!
0 votes
If I have the following block of code:

AuthenticationSection authentication = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication");
if (authentication.Mode == AuthenticationMode.None)
    context.AuthenticateRequest += Application_AuthenticateRequest;

And I want to verify that AuthenticateRequest did not have an event handler assigned to it how would I do that under the new API? My best guess doesn't seem to work:

// Arrange
AuthenticationModule target = new AuthenticationModule();

var context = Isolate.Fake.Instance<HttpApplication>();
var authenticationSection = Isolate.Fake.Instance<AuthenticationSection>();
Isolate.WhenCalled(() => authenticationSection.Mode).WillReturn(AuthenticationMode.Forms);

// Act

// Assert
Isolate.Verify.WasNotCalled(() => context.AuthenticateRequest);

asked by (1.8k points)

1 Answer

0 votes
Hi Colin,

Verification of event subscription can be done using the non-public API. The verification is done against the add method of the event.

For example:
public class ClassWithEvent
    public event EventHandler SomeEvent;

[TestMethod, Isolated]
public void VerifyEventWasNotSubscribed()
    var eventClass = Isolate.Fake.Instance<ClassWithEvent>();

     // The next line will break the test
     //eventClass.SomeEvent += (sender, args) => { };

    Isolate.Verify.NonPublic.WasNotCalled(eventClass, "add_SomeEvent");

Please let me know if it helps.

Best Regards,
Typemock Support Team
answered by (12k points)