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 Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hello,
I want to write unit test for a function whose only duty is to unsubscribe from events. I tried to use ExpectRemoveEvent function but I have gotten exceptions. The situation is I have "ClassA" that creates "ClassB" instance in the constructor (not takes "ClassB" instance as a parameter) and subscribes to "ClassB" events in the constructor ; and in function "Fct" I unsubscribe from "ClassB" events. I tried to use MockAll or Mock for ClassB in my test function, but it didn't work. Could you tell me waht to do?
Thanx.
asked by mkg (24.3k points)

2 Answers

0 votes
Hi,

You can do that using the Arrange Act Assert private API:
When you create an event the compiler generate two private methods that are called when you use the operators += and -=
"add_YourEventName" and "remove_YourEventName" you can do the verification on those methods like in the example below:

[Test, Isolated]
public void Test()
{
    var fake = Isolate.Fake.Instance<ClassB>();
    Isolate.Swap.NextInstance<ClassB>().With(fake);

    ClassA consumer = new ClassA();
    consumer.Unsubscribe();
    Isolate.Verify.NonPublic.WasCalled(fake, "remove_SomeEvent");
}


Please let me know if it helps.
answered by ohad (35.5k points)
0 votes
yes, that worked.
Thanx
answered by mkg (24.3k points)
...