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

Mocking an object passed to another thread

3 Answers 146 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 22 Aug 2012, 02:44 PM
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
}

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 24 Aug 2012, 06:59 PM
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.

0
Alex
Top achievements
Rank 1
answered on 24 Aug 2012, 07:14 PM
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
0
Ricky
Telerik team
answered on 29 Aug 2012, 05:29 PM
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.

Tags
General Discussions
Asked by
Alex
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Alex
Top achievements
Rank 1
Share this question
or