Created
November 3, 2010 19:52
-
-
Save gamlerhart/661599 to your computer and use it in GitHub Desktop.
async&unit-test: Simple Result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public string AwesomeBusinessOperation() | |
{ | |
var firstPart = TakesALongTimeToProcess("Tons"); | |
var secondPart = TakesALongTimeToProcess(firstPart+" of "); | |
var result = TakesALongTimeToProcess(secondPart+"money"); | |
return result; | |
} | |
private string TakesALongTimeToProcess(string word) | |
{ | |
// This operation takes a while | |
Thread.Sleep(1000); | |
return word; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<string> AwesomeBusinessOperationAsync() | |
{ | |
var firstPart = await TakesALongTimeToProcessAsync("Tons"); | |
var secondPart = await TakesALongTimeToProcessAsync(firstPart + " to "); | |
var result = await TakesALongTimeToProcessAsync(secondPart + "money"); | |
return result; | |
} | |
private Task<string> TakesALongTimeToProcessAsync(string word) | |
{ | |
// Remember, this is just a simulation | |
// Usually you would use some other async API here | |
return TaskEx.Run(() => | |
{ | |
// This operation takes a while | |
Thread.Sleep(1000); | |
return word; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void ExpectTonsOfMoney(){ | |
var toTest = new MyBusinessLogic(); | |
var result = toTest.AwesomeBusinessOperation(); | |
Assert.AreEqual("Tons of money",result); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void ExpectTonsOfMoney(){ | |
var toTest = new MyBusinessLogic(); | |
var result = toTest.AwesomeBusinessOperationAsync(); | |
Assert.AreEqual("Tons of money",result.Result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment