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 the following test:

[Test]
public void Load()
{
    INestedUnitOfWork unitOfWork = Isolate.Fake.Instance<INestedUnitOfWork>();
    IAmazonCredentials credentials = Isolate.Fake.Instance<IAmazonCredentials>();
    AmazonCredentialsForm view = Isolate.Fake.Instance<AmazonCredentialsForm>();

    WizardPresenter wp = Isolate.Fake.Instance<WizardPresenter>();
    Isolate.Swap.NextInstance<WizardPresenter>().With(wp);

    AmazonCredentialsFormPresenter presenter = new AmazonCredentialsFormPresenter(unitOfWork, credentials, view);
    Isolate.Invoke.Event(() => view.Load += null);

    Isolate.Verify.WasCalledWithAnyArguments(() => wp.ConfigureButtons());
    Isolate.Verify.WasCalledWithAnyArguments(() => view.NameTextEdit.Focus());
}


The view.Load event is wired up in the constructor of the AmazonCredentialsFormPresenter to a private function:
void mView_Load(object sender, EventArgs e)
{
    mWizardPresenter.ConfigureButtons();
    mView.NameTextEdit.Focus();
}


The test is failing on the Isolate.Invoke.Event line with a System.NullReferenceException. I've tried a few variations, such as:
Isolate.Invoke.Event(() => view.Load += null, EventArgs.Empty);
and
Isolate.Invoke.Event(() => view.Load += null, this, EventArgs.Empty);
but all give the same result.


What am I missing here?
asked by (3.5k points)

15 Answers

0 votes
Hi,

There are few issues here:
First it looks like a bug from our side - You should not get NullReferenceExceprion.
I'll need more details to re-produce the error, I'll send you mail from our support and we'll continue to investigate it offline.

Second the test will not pass even without the exception.
When you are faking the AmazonCredentialsForm you are you are using the default of Isolate.Fake.Instance() which is to recursively fake all the methods and properties of this class. when you wand to add an event handler inside the constructor of AmazonCredentialsFormPresenter the event adder is faked and the handler is not wired to the event.

You have few option to solve this:
1. Specify explicitly that the event adder should be called:
AmazonCredentialsForm view = Isolate.Fake.Instance<AmazonCredentialsForm>();
Isolate.WhenCalled(() => view.Load += null).CallOriginal();


2. create the fake AmazonCredentialsForm with Members.CallOriginal
AmazonCredentialsForm view = Isolate.Fake.Instance<AmazonCredentialsForm>(Members.CallOriginal);
answered by (35.5k points)
0 votes
I am also experiencing a similar issue. Getting a NullReferenceException when calling Invoke.Event. However, I'm dealing with an interface.

Taking the example from the docs to show my case (Can't provide my actual code but this example reproduces the NullRef exception):

public interface ILogger
{
    event EventHandler LogEntryCreated;
}

public class ComponentThatUsesLogger
{
    public ComponentThatUsesLogger(ILogger logger)
    {
        Logger = logger;
    }

    public ILogger Logger { get; set; }

    public bool EventRaised { get; private set; }

    public void Register()
    {
        Logger.LogEntryCreated += (o, e) => EventRaised = true;
    }
}

[TestMethod]
public void Parse_AAndHMessageFromSampleFile_ThreeMessagesParsed()
{
    var logger = Isolate.Fake.Instance<ILogger>();
    var component = new ComponentThatUsesLogger(logger);

    component.Register();

    Isolate.Invoke.Event(() => logger.LogEntryCreated += null, this, EventArgs.Empty);

    Assert.IsTrue(component.EventRaised);
}


I don't have the option of using CallOriginal since I'm dealing with the ILogger interface. How would I do this?
answered by (2.8k points)
0 votes
Hi,

This looks like a bug from our side.
Thanks for reporting it, we will fix it and send you a patch.
answered by (35.5k points)
0 votes
Thanks Ohad...you'll have that tomorrow morning, right? :D

(Yes, I am kidding.)
answered by (3.5k points)
0 votes
Hi,

I have bumped into the same problem, with a call to:

Isolate.Invoke.Event()

Generating a NullReferenceException.

Anyone know when a fix will be available?

Mats
answered by (3.9k points)
0 votes
Hi Mats,

Just to make sure, did it happen on a fake of interface or abstract class?

Regards,
Elisha
Typemock Support
answered by (12k points)
0 votes
Hi Elisha,

It happens both on an interface:

IAssessment assessment = Isolate.Fake.Instance<IAssessment>();
Isolate.Invoke.Event(() => assessment.CurrentProcessChanged += null, null, null);

and when using an actual object instead:

IAssessment assessment = DsAssessment.GenerateTestContent();
Isolate.Invoke.Event(() => assessment.CurrentProcessChanged += null, null, null);

Should I do something else or is this a bug?

Cheers Mats
answered by (3.9k points)
0 votes
Hi Mats,

You're using it correctly, this is in fact the same bug. We're already working on fixing it. The fix will be available in the the upcoming release.

Regards,
Elisha
Typemock Support
answered by (12k points)
0 votes
Hi All,

The issue of invoking event that is defined in an interface was resolved in version 5.4.4.
answered by (35.5k points)
0 votes
I am using TM 5.4.5 and I am still having problems firing an event defined on an interface. I think this test should pass and it does not (it compiles and runs, but I don't think the event handler code is ever executed).

public void TestProblem () {

   IRootCollection rootCollection = Isolate.Fake.Instance<IRootCollection> ();
   bool fired = false;
   rootCollection.CadContextChanged += new PropertyChangeEventHandler ((sender, args) => { fired = true; });
   Isolate.Invoke.Event (() => rootCollection.CadContextChanged += null, null, null);
   Assert.IsTrue (fired);
}


What am I doing wrong?
answered by (5.7k points)
...