Created
November 3, 2010 20:28
-
-
Save gamlerhart/661651 to your computer and use it in GitHub Desktop.
side-effect
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 event Action<string> OperationFinishedNotification; | |
public string AwesomeBusinessOperation() | |
{ | |
var firstPart = TakesALongTimeToProcess("Tons"); | |
var secondPart = TakesALongTimeToProcess(firstPart + " of "); | |
var result = TakesALongTimeToProcess(secondPart + "money"); | |
var eventToFire = OperationFinishedNotification; | |
if(null!=eventToFire) | |
{ | |
OperationFinishedNotification(result); | |
} | |
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 event Action<string> OperationFinishedNotification; | |
public async Task<string> AwesomeBusinessOperationAsync() | |
{ | |
var firstPart =await TakesALongTimeToProcess("Tons"); | |
var secondPart = await TakesALongTimeToProcess(firstPart + " of "); | |
var result = await TakesALongTimeToProcess(secondPart + "money"); | |
var eventToFire = OperationFinishedNotification; | |
if(null!=eventToFire) | |
{ | |
OperationFinishedNotification(result); | |
} | |
return result; | |
} | |
private Task<string> TakesALongTimeToProcess(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 ExpectEventToBeFired(){ | |
var toTest = new MyBusinessLogic(); | |
var eventFiredExpected = ""; | |
toTest.OperationFinishedNotification | |
+= eventArgument => | |
{ | |
eventFiredExpected = eventArgument; | |
}; | |
var result = toTest.AwesomeBusinessOperation(); | |
Assert.AreEqual("Tons of money",eventFiredExpected); | |
} |
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 ExpectEventToBeFired(){ | |
var toTest = new MyBusinessLogic(); | |
var eventFiredExpected = ""; | |
toTest.OperationFinishedNotification | |
+= eventArgument => | |
{ | |
eventFiredExpected = eventArgument; | |
}; | |
var result = toTest.AwesomeBusinessOperationAsync(); | |
// just wait for the asynchronous operation | |
result.Wait(); | |
Assert.AreEqual("Tons of money",eventFiredExpected); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment