Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
78 changes: 33 additions & 45 deletions TUnit.Mocks.Assertions/MockAssertionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ public static class MockAssertionExtensions
/// Asserts that the mock member was called at least once.
/// Generates the <c>WasCalled()</c> assertion on <see cref="IAssertionSource{ICallVerification}"/>.
/// </summary>
[GenerateAssertion(ExpectationMessage = "to have been called")]
public static AssertionResult WasCalled(ICallVerification verification)
[GenerateAssertion(ExpectationMessage = "to have been called at least once")]
public static AssertionResult WasCalled<T>(T verification) where T : ICallVerification
{
return WasCalled(verification, Times.AtLeastOnce);
}

/// <summary>
/// Asserts that the mock member was called the specified number of times.
/// Generic overload for types implementing <see cref="ICallVerification"/> (e.g. PropertyMockCall).
/// </summary>
[GenerateAssertion(ExpectationMessage = "to have been called {times} times")]
public static AssertionResult WasCalled<T>(
this T verification,
Times times) where T : ICallVerification
{
if (verification is null)
{
Expand All @@ -26,60 +38,36 @@ public static AssertionResult WasCalled(ICallVerification verification)

try
{
verification.WasCalled();
verification.WasCalled(times);
return AssertionResult.Passed;
}
catch (MockVerificationException ex)
{
return AssertionResult.Failed(ex.Message);
return AssertionResult.Failed(ex.Message, ex);
}
}

/// <summary>
/// Asserts that the mock member was called the specified number of times.
/// </summary>
public static WasCalledAssertion WasCalled(
this IAssertionSource<ICallVerification> source,
Times times,
[CallerArgumentExpression(nameof(times))] string? expression = null)
{
source.Context.ExpressionBuilder.Append($".WasCalled({expression})");
return new WasCalledAssertion(source.Context, times);
}

/// <summary>
/// Asserts that the mock member was called the specified number of times.
/// Generic overload for types implementing <see cref="ICallVerification"/> (e.g. PropertyMockCall).
/// </summary>
public static WasCalledAssertion WasCalled<T>(
this IAssertionSource<T> source,
Times times,
[CallerArgumentExpression(nameof(times))] string? expression = null) where T : ICallVerification
{
source.Context.ExpressionBuilder.Append($".WasCalled({expression})");
var mappedContext = source.Context.Map<ICallVerification>(v => v);
return new WasCalledAssertion(mappedContext, times);
}

/// <summary>
/// Asserts that the mock member was never called.
/// </summary>
public static WasNeverCalledAssertion WasNeverCalled(
this IAssertionSource<ICallVerification> source)
{
source.Context.ExpressionBuilder.Append(".WasNeverCalled()");
return new WasNeverCalledAssertion(source.Context);
}

/// <summary>
/// Asserts that the mock member was never called.
/// Generic overload for types implementing <see cref="ICallVerification"/> (e.g. PropertyMockCall).
/// </summary>
public static WasNeverCalledAssertion WasNeverCalled<T>(
this IAssertionSource<T> source) where T : ICallVerification
[GenerateAssertion(ExpectationMessage = "to not have been called")]
public static AssertionResult WasNeverCalled<T>(
this T verification) where T : ICallVerification
{
source.Context.ExpressionBuilder.Append(".WasNeverCalled()");
var mappedContext = source.Context.Map<ICallVerification>(v => v);
return new WasNeverCalledAssertion(mappedContext);
if (verification is null)
{
return AssertionResult.Failed("Verification target is null");
}

try
{
verification.WasNeverCalled();
return AssertionResult.Passed;
}
catch (MockVerificationException ex)
{
return AssertionResult.Failed(ex.Message, ex);
}
}
}
43 changes: 0 additions & 43 deletions TUnit.Mocks.Assertions/WasCalledAssertion.cs

This file was deleted.

39 changes: 0 additions & 39 deletions TUnit.Mocks.Assertions/WasNeverCalledAssertion.cs

This file was deleted.

Loading
Loading