Hello i am unit testing a method which deals some I/O
operations.I was wondering if there is some kind of way to pause that method for a specific time , at a specific line , without altering the original code.
Original Method
public async Task<int> SomeMethodAsync()
{
var x=DoSomething1();
int value=await DoSomething2Async();
return value;
}
Look at the method above.Is there any way from a TestCase
to add a delay or a pause at a specific location in the target method? I would like my method to become something like this Only when unit testing !:
Desired method when unit testing
public async Task<int> SomeMethodAsync()
{
var x=DoSomething1(); //line index=0
# await Task.Delay(delay) //delay set in the testcase
int x=await DoSomething2Async(); //line index=1
return x;
}
Unit Test
[TestCase]
public void TestSomeMethodWithDelay()
{
var delay=1000;
int desiredLineIndex=1;
int result=await SomeMethodAsync();
}
As you can see i would like when testing a method to insert some code before a target line , in my case an await Task.Delay(interval)
where interval
is set in the TestCase
. I do not want to modify/pollute the source code with preprocessor directives for testing.The only other way i could think is to use something like System.Reflection.Emit
and rewrite the target method when testing.
Is there some other elegant way?
P.S I just want , when running a particular set of tests to insert a delay between two lines of code
without altering the original code.
Update The SomeMethodAsync
is from another dll
.The code must not be changed ! The DoSomething2Async
is an external call.
Aucun commentaire:
Enregistrer un commentaire