Mocking an object passed to another thread

4 posts, 0 answers
  1. Alex
    Alex avatar
    3 posts
    Member since:
    Aug 2012

    Posted 22 Aug 2012 Link to this post

    I have a class that takes a mocked object as a parameter.  When the class is invoked on the current thread, calls on the mocked object do nothing by virtue of the mock.  However if the class is run on another thread, the mocked object calls the actual methods instead of doing nothing.  Are mocked objects supported on background threads?

    public class BackgroundThread 
    {
      private Widget _widget;
      public BackgroundThread(Widget widget)
      {
        _widget = widget;
      }
      
      public void Run()
      {
        Console.WriteLine("widget="+_widget);
        _widget.DoSomething();
      }
    }
      
    public class Widget
    {
      public void DoSomething()
      {
        throw new Exception("Not ready to do something");
      }
    }
      
    // Test Class
    [TestMethod]
    public void TestBackgroundThread()
    {
      var widget = Mock.Create<Widget>();
      var bt = new BackgroundThread(widget);
      var thread = new Thread(bt.Run);
      thread.Start();
      Console.WriteLine("Thread started");
      thread.Join();  // Exception is thrown but should not because object was mocked
    }
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 24 Aug 2012 Link to this post

    Hi Alex,
    Thanks again for contacting us. We have identified the issue and if you please open up a support ticket then I can send you the latest build.

    Kind Regards
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft R3 2016 release webinar banner
  4. Alex
    Alex avatar
    3 posts
    Member since:
    Aug 2012

    Posted 24 Aug 2012 Link to this post

    I am unable to get mocking working unless using interfaces or methods marked virtual. I have tried uninstalling and reinstalling justmock. Currently I am using vs 2010, resharper, ncover and just code test runner. Correct me if I am wrong but this product supports these features in both the free and commercial versions? I downloaded the trial and can't tell if I'm using the free or commercial version. I have opened a support ticket already. Thanks, Alex
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 29 Aug 2012 Link to this post

    Hi Alex,
    Thanks again for your reply.

    I would recommend you to use either TestDriven.Net / MSTest / JustCode test runner. We still don't support R# test runner officially and hoping to do that in Q3 release. 

    So far things are working as expected when run in a recommended environment.  However, I made a fix to the background thread issue that you sent previously. If you require that fix then I would request you to create a support ticket with same subject and context so that I am able to send an internal build.

    Kind Regards
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top