Skip to content

Commit ce7624f

Browse files
committed
Add .Passing() tests for OkTestBuilder
1 parent b4b4a32 commit ce7624f

File tree

1 file changed

+53
-0
lines changed
  • test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/OkTests

1 file changed

+53
-0
lines changed

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/OkTests/OkTestBuilderTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Setups;
99
using Setups.Common;
1010
using Setups.Controllers;
11+
using Utilities;
1112
using Xunit;
1213

1314
public class OkTestBuilderTests
@@ -424,5 +425,57 @@ public void AndAlsoShouldWorkCorrectly()
424425
TestObjectFactory.GetOutputFormatter(),
425426
new CustomOutputFormatter()));
426427
}
428+
429+
[Fact]
430+
public void PassingShouldCorrectlyRunItsAssertionFunction()
431+
{
432+
MyController<MvcController>
433+
.Instance()
434+
.Calling(c => c.FullOkAction())
435+
.ShouldReturn()
436+
.Ok(ok => ok
437+
.Passing(o => o.Formatters?.Count == 2));
438+
}
439+
440+
[Fact]
441+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
442+
{
443+
Test.AssertException<InvocationResultAssertionException>(
444+
() =>
445+
{
446+
MyController<MvcController>
447+
.Instance()
448+
.Calling(c => c.FullOkAction())
449+
.ShouldReturn()
450+
.Ok(ok => ok
451+
.Passing(o => o.Formatters?.Count == 0));
452+
},
453+
$"When calling {nameof(MvcController.FullOkAction)} " +
454+
$"action in {nameof(MvcController)} expected the OkObjectResult to pass the given predicate, but it failed.");
455+
}
456+
457+
[Fact]
458+
public void PassingShouldCorrectlyRunItsAssertionAction()
459+
{
460+
MyController<MvcController>
461+
.Instance()
462+
.Calling(c => c.FullOkAction())
463+
.ShouldReturn()
464+
.Ok(ok => ok
465+
.Passing(o =>
466+
{
467+
const int expectedFormattersCount = 2;
468+
var actualFormattersCount = o.Formatters?.Count;
469+
if (actualFormattersCount != expectedFormattersCount)
470+
{
471+
throw new InvalidAssertionException(
472+
string.Format("Expected {0} to have {1} {2}, but it has {3}.",
473+
o.GetType().ToFriendlyTypeName(),
474+
expectedFormattersCount,
475+
nameof(o.Formatters),
476+
actualFormattersCount));
477+
};
478+
}));
479+
}
427480
}
428481
}

0 commit comments

Comments
 (0)