.ShouldThrow<T> and .ShouldThrow<T>().WithMessage throw FileNotFoundException
See original GitHub issueHello everyone,
I’m right in the middle of unit testing my Windows Store App which I recently upgraded to be compatible with Windows 8.1. For that I’ve got a bunch of Windows Store Libraries which I’m testing together with NUnit 2.6.3, FluentAssertions 2.1.0.0 (the WinRT version) and FakeItEasy 1.14.0.0 - using Visual Studio 2013.
In most of my Service classes I throw ArgumentNullExceptions which I’d love to test. After Googling for a while I found a post somewhere mentioning that I should test Exceptions by wrapping the method under test inside an Action like so:
public void TestGetMatchAsyncWithEmptyMatchId()
{
// Arrange
var matchWorker = new MatchWorker();
// Act
Action action = () => matchWorker.GetMatchAsync(string.Empty);
// Assert
action.ShouldThrow<ArgumentException>("Expected GetMatchAsync to throw ArgumentException");
//.WithMessage("MatchId can't be null or empty");
}
where as the method under test looks like
public async Task<Match> GetMatchAsync(string matchId)
{
if (string.IsNullOrEmpty(matchId))
throw new ArgumentException("MatchId can't be null or empty", "matchId");
/* .... */
}
Now, no matter if I execute my tests via the ReSharper 8.0 Test-Runner or straight from the NUnit Test-Runner my test will fail
System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.VisualStudio.TestPlatform.UnitTestFramework, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
at FluentAssertions.Execution.AssertionHelper.Throw(String message)
at FluentAssertions.Execution.AssertionScope.FailWith(String failureMessage, Object[] failureArgs) in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\Execution\AssertionScope.cs: line 168
at FluentAssertions.Specialized.ActionAssertions.ShouldThrow(String reason, Object[] reasonArgs) in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\Specialized\ActionAssertions.cs: line 41
at VERSUS.Communication.Test.Worker.MatchWorkerTest.TestGetMatchAsyncWithEmptyMatchId() in MatchWorkerTest.cs: line 73
Unfortunately I could only find v12 of the Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll
on my machine which FluentAssertions doesn’t want to accept ;p
Speaking of .ShouldThrow<T>().WithMessage()
: The assertion will fail if you throw an ArgumentException
with the name of the invalid parameter. I.e.:
Will fail:
throw new ArgumentException("MatchId can't be null or empty", "matchId");
Will work:
throw new ArgumentException("MatchId can't be null or empty");
So I also tried throwing the ArgumentException
in my method under test without the name of the param which unfortunately didn’t work as well.
Is it possible that .ShouldThrow
doesn’t work properly on async/await methods? =/
Changing the Action to something like didn’t work too.
Action action = async () => await matchWorker.GetMatchAsync(string.Empty);
Any help is greatly appreciated =)
You’ve did an awesome job with your library so far!
Cheers~
Issue Analytics
- State:
- Created 10 years ago
- Comments:18 (10 by maintainers)
Top GitHub Comments
I’ve just tried a Windows Store 8.1 unit test project from inside Visual Studio 2013 using the NuGet package for FA 2.1 and it just worked. One thing that surprised me is that you are using R# 8. Only R# 8.1 supports Visual Studio 2013.
Oh, and to test async methods, rewrite your action like this:
Fixed it in the latest commit. I now added automatic detection of the PCL version of NUnit. If that’s not available, it’ll fall back to the MSTest framework.