Skip to content

Commit

Permalink
[Issue #24] Throw exception when file path is null
Browse files Browse the repository at this point in the history
  • Loading branch information
theramis committed Mar 15, 2020
1 parent 5e95def commit 269ecf2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
8 changes: 8 additions & 0 deletions docs/pages/faqs.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ There are currently three solutions for this issue.
```

For more information see this issue: [theramis/snapper#16](https://github.com/theramis/Snapper/issues/16)

### Why am I getting a `UnableToDetermineTestFilePathException`?
```
Snapper.Exceptions.UnableToDetermineTestFilePathException : Unable to determine the file path of the test method.
Make sure optimisation of the test project is disabled. See https://theramis.github.io/Snapper/#/pages/faqs for more info.
```

This can happen when the PDB files are not generated for the test project. Try setting the `<Optimize>false</Optimize>` or `<DebugType>full</DebugType>` settings in your projects csproj file. See above for more details on how to set these.
5 changes: 5 additions & 0 deletions project/Snapper/Core/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@ public const string TestMethodNotFoundMessage

public const string MalformedJsonSnapshotMessage
= "The snapshot provided contains malformed JSON. See inner exception for details.";

public const string UnableToDetermineTestFilePathMessage
= "Unable to determine the file path of the test method. " +
"Make sure optimisation of the test project is disabled. " +
"See https://theramis.github.io/Snapper/#/pages/faqs for more info.";
}
}
37 changes: 27 additions & 10 deletions project/Snapper/Core/TestMethodResolver/TestMethodResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
Expand All @@ -23,19 +24,30 @@ private static IEnumerable<ITestMethod> GetTestMethods(MemberInfo method, string

public ITestMethod ResolveTestMethod()
{
var stackTrace = new StackTrace(1, true);
foreach (var stackFrame in stackTrace.GetFrames() ?? new StackFrame[0])
try
{
var method = stackFrame.GetMethod();
var stackTrace = new StackTrace(1, true);
foreach (var stackFrame in stackTrace.GetFrames() ?? new StackFrame[0])
{
var method = stackFrame.GetMethod();

if (IsAsyncMethod(method))
method = GetMethodBaseOfAsyncMethod(method);
if (IsAsyncMethod(method))
method = GetMethodBaseOfAsyncMethod(method);

if (IsSnapperMethod(method))
continue;
if (IsSnapperMethod(method))
continue;

if (TryGetTestMethod(method, stackFrame.GetFileName(), out var testMethod))
return testMethod;
if (TryGetTestMethod(method, stackFrame.GetFileName(), out var testMethod))
return testMethod;
}
}
catch (UnableToDetermineTestFilePathException)
{
throw;
}
catch (Exception ex)
{
throw new SupportedTestMethodNotFoundException(ex);
}

throw new SupportedTestMethodNotFoundException();
Expand Down Expand Up @@ -74,6 +86,11 @@ private static bool TryGetTestMethod(MemberInfo method, string fileName, out ITe
if (tm.IsTestMethod())
{
testMethod = tm;

if (fileName == null)
{
throw new UnableToDetermineTestFilePathException();
}
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ public SupportedTestMethodNotFoundException()
: base(Messages.TestMethodNotFoundMessage)
{
}

public SupportedTestMethodNotFoundException(Exception baseException)
: base(Messages.TestMethodNotFoundMessage, baseException)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Snapper.Core;

namespace Snapper.Exceptions
{
internal class UnableToDetermineTestFilePathException : Exception
{
public UnableToDetermineTestFilePathException()
: base(Messages.UnableToDetermineTestFilePathMessage)
{
}
}
}

0 comments on commit 269ecf2

Please sign in to comment.