How to mock events when using Future Mocking?

8 posts, 0 answers
  1. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 30 Apr 2014 Link to this post

    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.
  2. Todor
    Admin
    Todor avatar
    63 posts

    Posted 01 May 2014 Link to this post

    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.

     
  3. DevCraft R3 2016 release webinar banner
  4. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 02 May 2014 in reply to Todor Link to this post

    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.
  5. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 06 May 2014 in reply to Nacho Link to this post

    Hello,

    any update on this?

    Thanks.
  6. Todor
    Admin
    Todor avatar
    63 posts

    Posted 06 May 2014 Link to this post

    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.

     
  7. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 06 May 2014 in reply to Todor Link to this post

    Ok, no worries, at least now I know you didn't forget me :)
  8. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 07 May 2014 Link to this post

    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.

     
  9. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 07 May 2014 in reply to Stefan Link to this post

    Thanks, that solves completely the issue.

    Regards.
Back to Top
DevCraft R3 2016 release webinar banner