This is a migrated thread and some comments may be shown as answers.

How to mock events when using Future Mocking?

7 Answers 25 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nacho
Top achievements
Rank 1
Nacho asked on 30 Apr 2014, 09:46 AM
Hello,

I need to test the handler of a Timer in one class. We are using Future Mocking to mock the Timer inside it.

Is there a way to use the Raise together with the .IgnoreInstance() method? I haven't found it.

Regards.

7 Answers, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 01 May 2014, 01:10 PM
Hello Ignacio,

I am sorry that you have troubles using JustMock.

I know of three different Timers types in BCL: System.Windows.Forms.Timer, System.Timers.Timer and System.Threading.Timer. Which one are you using? I need this information to try and reproduce the behavior on our side.

Regards,
Todor
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Nacho
Top achievements
Rank 1
answered on 02 May 2014, 06:56 AM
Hello,

the main issue I'm struggling with is that when you do:

Mock.Raise(xxx,xxx)

then you cannot put .IgnoreInstance() because the return type of the Raise() method is different from the Arrange() so I cannot use Future Mocking together with Raise().

What I want to do is something like:

// Arrange
TestedClass testedClass = new TestedClass();
 
Mock.Arrange(() => new Timer()).DoNothing();
 
// Act
Mock.Raise(() => new Timer().Elapsed += null, new ElapsedEventArgs()).IgnoreInstance();

That way I will be able to test the eventHandler of the timer inside the TestedClass.

Anyway we are using Timers.Timer just in case it helps.

Regards.
0
Nacho
Top achievements
Rank 1
answered on 06 May 2014, 07:53 AM
Hello,

any update on this?

Thanks.
0
Todor
Telerik team
answered on 06 May 2014, 03:29 PM
Hi Ignacio,

Due to a big holiday in Bulgaria we have reduced support capacity. We will investigate further your issue and will write back to you as soon as possible. I am really sorry for the inconvenience caused!
 
Regards,
Todor
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Nacho
Top achievements
Rank 1
answered on 06 May 2014, 03:37 PM
Ok, no worries, at least now I know you didn't forget me :)
0
Stefan
Telerik team
answered on 07 May 2014, 06:45 AM
Hello Ignacio,

I'm sorry for keeping you waiting for so long.

You still need an instance on which to raise the event. Raising the event on all existing timers is likely impractical. I suggest that you use the arrangement on the new expression to get the instance on which to raise the event, like so:
// Arrange
Timer timer = null;
Mock.Arrange(() => new Timer()).DoInstead((Timer thisTimer) => timer = thisTimer);
// Act
// first, do the thing that creates the timer
Timer.Do();
// we now have a reference to the timer from the above DoInstead clause
Mock.Raise(() => timer.TheEvent += null);
In the above code I get the reference to the created timer using future constructor mocking. Then, I can raise the event on the instance regularly.

I hope this is enough to get your test running.

Cheers!
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Nacho
Top achievements
Rank 1
answered on 07 May 2014, 07:47 AM
Thanks, that solves completely the issue.

Regards.
Tags
General Discussions
Asked by
Nacho
Top achievements
Rank 1
Answers by
Todor
Telerik team
Nacho
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or