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
I have a class, A, that contains an object of a class B. The B class can fire an event that is handled by A.

public class B
{
    public Event EventHandler SomethingHappened;
}

public class A
{
    private B _b;

    public class A(B b)
    {
        _b = b;
        _b.SomethingHappened += OnSomethingHappened();
    }

    public void OnSomethingHappened()
    {
    }
}


I would like to test all of this, and I would like to test fire the event in B so that my faked instance of A can process it:

var fakeB = Isolate.Fake.Instance<B>(Members.CallOriginal);
Isolate.Swap.AllInstances<B>().With(fakeB);

var fakeA = Isolate.Fake.Instance<A>(Members.CallOriginal, ConstructorWillBe.Called, fakeB);
Isolate.Swap.AllInstances<A>().With(fakeA);

Isolate.Invoke.Event(() => fakeB.SomethingHappened += null, /*event parameter*/);


This doesn't seem to work. If I put a breakpoint at A.OnSomethingHappened(), it is never hit. Any ideas as to why?
asked by JeffFerguson (3.2k points)

3 Answers

0 votes
Try the following:
[Isolated]//important
[Test]
public void MyTest()
{
   var myB = new B();
   var underTest = new A(myB());
   
   Isolate.Invoke.Event(() => myB.SomethingHappened += null, /*event parameter*/);

   Isolate.Verify.WasCalledWithAnyArgument(()=>underTest.OnSomethingHappened());
}
answered by scott (32k points)
0 votes
So ... you want me to create instances of A and B without using Isolate.Fake.Instance?
answered by JeffFerguson (3.2k points)
0 votes
You can if you want to, but you don't need to
answered by scott (32k points)
...