Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, June 15, 2016 9:08 AM
I am currently reading Xamarin Cross Platform Application Development (https://www.packtpub.com/application-development/xamarin-cross-platform-application-development-second-edition) up to page 84 about testing the NUnit project.
I am using Visual Studio 2015 Community Edition with NUnit3 installed. However, the book uses the previous version of NUnit. I would like to know what will this function be if it written in NUnit3.
[Test, ExpectedException(typeof(Exception), ExpectedMessage = "Username is blank.")]
public async Task LoginWithNoUserNameOrPassword()
{
//Throws an exception.
await loginViewModel.Login();
}
All replies (4)
Thursday, June 16, 2016 12:47 AM
Bump!!!
Thursday, June 16, 2016 8:28 AM
From their docs:
For example, NUnit 3.0 no longer supports ExpectedExceptionAttribute. However, preferred alternatives Assert.Throws and the ThrowsConstraint have been available for several years. If you remove the attribute from your tests and use one of the alternatives instead, you can verify that they work in your present environment and they will continue to work after conversion.
You can find this here: https://github.com/nunit/docs/wiki/Upgrading
The ThrowsConstraint is detailed here: https://github.com/nunit/docs/wiki/ThrowsConstraint
So what you want would be something like:
[Test] public async Task LoginWithNoUserNameOrPassword() { Assert.That(() => await loginViewModel.Login(), Throws.TypeOf<Exception>().With.Property("Message").EqualTo("Username is blank.")); }
Thursday, June 16, 2016 2:38 PM
Thanks! That works perfectly. Although there was an error about await should be used within lambda function or something like that. I adjust the code a bit so it is like this.
[Test]
public async Task LoginWithNoUsernameOrPassword()
{
Assert.That(async () => await loginViewModel.Login(),
Throws.TypeOf<Exception>().With.Property("Message").EqualTo("Username is blank."));
}
I found the additional solution here http://stackoverflow.com/questions/20593501/the-await-operator-can-only-be-used-within-an-async-lambda-expression.
EDIT 1: How can I flag this thread as resolved?
Thursday, June 16, 2016 11:05 PM
@MikaelKristyawicaksono yup, true. Sorry - wrote the code in my head instead of in code so apologies for the mistake.