Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ List of added functionality in this release.

List of fixes in this release.

- Fixed JSInterop error message when trying to import an unconfigured module (#398).

- Fixed issue where a registered fall-back service provider was not made available to resolve service dependencies of components under test. Thanks to [@dady8889](https://github.com/dady8889) for the reporting the issue.

## [1.1.5] - 2021-04-30
Expand Down
38 changes: 25 additions & 13 deletions src/bunit.web/JSInterop/JSRuntimeUnhandledInvocationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Bunit
[Serializable]
public sealed class JSRuntimeUnhandledInvocationException : Exception
{
private const string DefaultImportIdentifier = "import";

/// <summary>
/// Gets the unplanned invocation.
/// </summary>
Expand Down Expand Up @@ -53,22 +55,29 @@ private static string CreateErrorMessage(JSRuntimeInvocation invocation)
sb.AppendLine("Configure bUnit's JSInterop to handle the call with following:");
sb.AppendLine();

if (invocation.IsVoidResultInvocation)
if (string.Equals(invocation.Identifier, DefaultImportIdentifier, StringComparison.InvariantCulture))
{
sb.AppendLine($" SetupVoid({GetArguments(invocation)})");
sb.AppendLine($" SetupModule({GetArguments(invocation, includeIdentifier: false)})");
}
else
{
sb.AppendLine($" Setup<{GetReturnTypeName(invocation.ResultType)}>({GetArguments(invocation)})");
}

if (invocation.Arguments.Any())
{
sb.AppendLine("or the following, to match any arguments:");
if (invocation.IsVoidResultInvocation)
sb.AppendLine($" SetupVoid(\"{invocation.Identifier}\", _ => true)");
{
sb.AppendLine($" SetupVoid({GetArguments(invocation)})");
}
else
sb.AppendLine($" Setup<{GetReturnTypeName(invocation.ResultType)}>(\"{invocation.Identifier}\", _ => true)");
{
sb.AppendLine($" Setup<{GetReturnTypeName(invocation.ResultType)}>({GetArguments(invocation)})");
}

if (invocation.Arguments.Any())
{
sb.AppendLine("or the following, to match any arguments:");
if (invocation.IsVoidResultInvocation)
sb.AppendLine($" SetupVoid(\"{invocation.Identifier}\", _ => true)");
else
sb.AppendLine($" Setup<{GetReturnTypeName(invocation.ResultType)}>(\"{invocation.Identifier}\", _ => true)");
}
}

sb.AppendLine();
Expand Down Expand Up @@ -113,11 +122,14 @@ private static string GetGenericInvocationArguments(JSRuntimeInvocation invocati
}
}

private static string GetArguments(JSRuntimeInvocation invocation)
private static string GetArguments(JSRuntimeInvocation invocation, bool includeIdentifier = true)
{
var args = invocation.Arguments
.Select(x => x is string s ? $"\"{s}\"" : x?.ToString() ?? "null")
.Prepend($"\"{invocation.Identifier}\"");
.Select(x => x is string s ? $"\"{s}\"" : x?.ToString() ?? "null");
if (includeIdentifier)
{
args = args.Prepend($"\"{invocation.Identifier}\"");
}

return string.Join(", ", args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,22 @@ public void Test035(string identifier)
Assert.Equal(exectedErrorMessage, sut.Message);
}

[Theory(DisplayName = "Message prints prints correctly when trying to import an unconfigured module")]
[AutoData]
public void Test036(string moduleName)
{
var identifier = "import";
var returnType = typeof(void);
var invocationMethodName = "InvokeAsync";
var exectedErrorMessage = CreateExpectedErrorMessage(
$"{CodeIdent}{invocationMethodName}<{returnType.Name}>(\"{identifier}\", \"{moduleName}\")",
$"{CodeIdent}SetupModule(\"{moduleName}\")");

var sut = new JSRuntimeUnhandledInvocationException(new JSRuntimeInvocation(identifier, new object?[] { moduleName }, returnType, invocationMethodName));

Assert.Equal(exectedErrorMessage, sut.Message);
}

public class Dummy1 { }
public class Dummy2 { }
public class Dummy3 { }
Expand Down