Skip to content

Instantly share code, notes, and snippets.

@hjerpbakk
Created October 1, 2013 19:37
Show Gist options
  • Save hjerpbakk/6783905 to your computer and use it in GitHub Desktop.
Save hjerpbakk/6783905 to your computer and use it in GitHub Desktop.
public class ViewModelWithAsyncMethodCaller {
private readonly AsyncMethodCaller asyncMethodCaller;
public ViewModelWithAsyncMethodCaller(AsyncMethodCaller asyncMethodCaller) {
this.asyncMethodCaller = asyncMethodCaller ?? new AsyncMethodCaller();
}
public string Message { get; set; }
public string Result { get; set; }
public void ExecuteAsync() {
Message = "Loading...";
asyncMethodCaller.CallMethodAndContinue(DoSomething, WorkCompleted, HandleError);
}
private string DoSomething() {
return "Result";
}
private void WorkCompleted(string result) {
Message = "Completed";
Result = result;
}
private void HandleError(Exception exception) {
Message = exception.Message;
}
}
[TestFixture]
public class ViewModelTests {
[Test]
public void DoSomething() {
var asyncMethodCaller = new TestAsyncMethodCaller();
var viewModel = new ViewModelWithAsyncMethodCaller(asyncMethodCaller);
viewModel.ExecuteAsync();
Assert.AreEqual("Loading...", viewModel.Message);
asyncMethodCaller.StartServiceAndWait();
Assert.AreEqual("Result", viewModel.Result);
Assert.AreEqual("Completed", viewModel.Message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment