chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
May i know is there any method to test thread?
asked by clteh9 (5.3k points)

7 Answers

0 votes
Hi
Can you please be more specific?
What are you trying to do?
answered by ohad (35.4k points)
0 votes
the thread is in the function that i want to test, for eg:

private Thread t1;

public void ProcThread(){}

public void StartThread()
{
... ... .. ..............
.........................
.........................
tl = nre Thread(new ThreadStart(ProcThread));
tl.Start();
}
answered by clteh9 (5.3k points)
0 votes
Hi

Yes there is a way
You can see a detailed explanation here
answered by ohad (35.4k points)
0 votes
Hihi,

i have read the explaination, so it is no need to mock thread and expect call - start() if i want to test startThread()?
answered by clteh9 (5.3k points)
0 votes
may i know what is MyThreadAction.StartAsyncAction() abt? thanks
answered by clteh9 (5.3k points)
0 votes
MyThreadAction.StartAsyncAction is just some code that starts an asynchronous action.
This can be done in multiple ways in .NET

1. Start an new Thread.
2. Start a Thread from the ThreadPool
3. Use the delegate BeginInvoke

The point is that the method returns immediately and we need to wait for some action to finish before we can assert if our test fails or not.

:arrow: one way to do it is by calling Process.Sleep();
but this might not work (if the sleep it too short) or might create longer tests then required (if the sleep is too long).

:arrow: Another is to call Thread.Join
but we might be testing code that the thread doesn't exit.

:idea: Using TypeMock it is quite simple. we mock a method that when called we will know that we are ready to assert out test.
This method is normally part of our test (for example test that the result passed is the correct result).
Now we use the VerifyWithTimeout and our test will run at the optimal time but will still fail if the we don't return in time.
answered by scott (32k points)
0 votes
how can we make sure the test return in time? if not return in time, will get TypeMock.VerifyException :
TypeMock Verification: Operation timeout, Not all expectations where called:
answered by clteh9 (5.3k points)
...