AsyncApprovals - rules and exceptions
[Contributors: James Counts]
In the end, all tests become synchronous. This means for a normal test we recommend
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
/// DON'T USE ASYNC | |
[TestMethod] | |
public async Task TestHttp() | |
{ | |
var response = await new HttpClient().GetAsync("http://www.google.com/"); | |
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); | |
} | |
/// Prefer to use .Result | |
[TestMethod] | |
public void TestHttp() | |
{ | |
var response = new HttpClient().GetAsync("http://www.google.com/").Result; | |
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); | |
} |
However, If you are looking to test exceptions everything changes and you might want to use
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 TestAsyncExceptions() | |
{ | |
int a = 0; | |
AsyncApprovals.VerifyException(async () => 1/a); | |
} | |
// NOTE: If async is creating inconsient stack traces, consider a scrubber | |
AsyncApprovals.VerifyException(async () => 1/a, | |
ScrubberUtils.RemoveLinesContaining("System.Linq.Parallel.QueryTask")); |
Removed BCL requirement
HttpClient is nice way of doing web calls in .Net. Unfortunately, at this time the BCL in nuget does unfortunate things to your project if you do not wish to use the HttpClient.
This is a violation of a core philosophy of approvaltests
"only pay for the dependencies you use"
HttpClient was add ApprovalTests 3.6. Thanks to Simon for pointing and troubleshooting this error. It has now been removed.
Wpf Binding Asserts
[Contributors: Jay Bazuzi]
This is a bonus from v3.6
It is a very hard thing to detect and report Wpf Binding Error. To even get the reports to happen you have to fiddle with the registry and then read and parse logs.
No More! Now to you use BindsWithoutError to ensure that your Wpf binding are working.
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
Binding myBinding = new Binding(TestViewModel.MyPropertyPropertyName); | |
WpfBindingsAssert.BindsWithoutError(viewModel, () => | |
{ | |
// ... create control and initalize bindings | |
// (This will happen once the reporting and logging has been setup) | |
var textBox = new TextBox(); | |
textBox.SetBinding(TextBox.TextProperty, myBinding); | |
return textBox; | |
}); | |
/************************************************ | |
If this fails you will get an exception like: | |
System.Windows.Data Error: 40 : BindingExpression path error: 'MyProperty' property not found on 'object' ''TestViewModel' (Hashcode)'. | |
BindingExpression:Path=MyProperty | |
DataItem='TestViewModel' (Hashcode) | |
target element is 'TextBox' (Name='') | |
target property is 'Text' (type 'String') | |
*************************************************/ |