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
0 votes
Hi,

[TestMethod]
Isolated]
public void JOBMANAGER_fires_a_CurrentJobfinished_event_when_a_JobFinished_event_is_triggerd_by_a_job()
{
//arrange
IJob job = new Job();
IJobManager manager = new JobManager("ignore", "ignore", "ingnore", null);
// Isolate.NonPublic.WhenCalled(manager, "Start").CallOriginal();
// Isolate.NonPublic.WhenCalled(manager, "OnCurrentJobFinished").CallOriginal();
// Isolate.NonPublic.WhenCalled(manager, "JobFinishedHandler").CallOriginal();
Isolate.NonPublic.WhenCalled(manager, "Impersonate").IgnoreCall();

bool eventTiggered = false;
manager.CurrentJobFinished += delegate
{
eventTiggered = true;
};

//act
manager.AddJob(job);
manager.Start();
// Isolate.Invoke.Event(() => job.JobFinished += null);

//assert
Assert.IsTrue(eventTiggered, "CurrentJobFinished event shoukd be triggered when a job finished");
}

This test failes and 1 sec later it works..
it fails on the assert method not on a internal method of job or jobmanager
asked by rolandf (640 points)

2 Answers

0 votes
Hi,

This case sometimes happen when the code under test is multithreaded. The cause is that the act and the assert are not synchronized. In this case, the assert is executed just before the act.

Does the JobManager class starts a new thread? If the Start method runs on background thread then sometimes it will be executed after the assert. If so, perhaps it is better to synchronize between the act and the assert.

If it is not the case, can you please send us a small project that reproduces this case?

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

Thanks for the reply.

Yes the jobmanager starts a job in a background thread with parallen extension api.

Are there some tricks how ik can wait until completion in the unittest?

Like Isolatior.WaitForAsynchCompletion();

;)

Roland
answered by rolandf (640 points)
...