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
}