question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.ShouldThrow<T> and .ShouldThrow<T>().WithMessage throw FileNotFoundException

See original GitHub issue

Hello 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:closed
  • Created 10 years ago
  • Comments:18 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
dennisdoomencommented, Dec 15, 2013

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:

Func<Task> asyncFunction = async () => { await matchWorker.GetMatchAsync(string.Empty); };  
0reactions
dennisdoomencommented, Dec 20, 2013

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

when to throw FileNotFoundException - java
When I compiled the program, I got an error at readFile() in the main method. It seems that I need to throw an...
Read more >
How to Throw Exceptions in Java
Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception...
Read more >
How to Throw & Handle Scala Exceptions
Here's how to respond to and investigate the cause of exceptions in Scala: 1. Try/Catch → 2. Finally Clauses → 3. Custom Exceptions....
Read more >
FileNotFoundException in Java
As indicated on Java's API documentation, this exception can be thrown when: A file with the specified pathname does not exist; A file...
Read more >
Create a Custom Exception in Java
While the code throws FileNotFoundException, it's not clear what the exact cause is — whether the file doesn't exist or the file name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found