Skip to content

Commit b4b4a32

Browse files
committed
Add .Passing() tests for ObjectTestBuilder
1 parent 11c1eb1 commit b4b4a32

File tree

1 file changed

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

1 file changed

+54
-0
lines changed

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/ObjectTests/ObjectTestBuilderTests.cs

Lines changed: 54 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 ObjectTestBuilderTests
@@ -422,5 +423,58 @@ public void AndAlsoShouldWorkCorrectly()
422423
TestObjectFactory.GetOutputFormatter(),
423424
new CustomOutputFormatter()));
424425
}
426+
427+
428+
[Fact]
429+
public void PassingShouldCorrectlyRunItsAssertionFunction()
430+
{
431+
MyController<MvcController>
432+
.Instance()
433+
.Calling(c => c.FullObjectResultAction())
434+
.ShouldReturn()
435+
.Object(obj => obj
436+
.Passing(o => o.Formatters?.Count == 2));
437+
}
438+
439+
[Fact]
440+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
441+
{
442+
Test.AssertException<InvocationResultAssertionException>(
443+
() =>
444+
{
445+
MyController<MvcController>
446+
.Instance()
447+
.Calling(c => c.FullObjectResultAction())
448+
.ShouldReturn()
449+
.Object(obj => obj
450+
.Passing(o => o.Formatters?.Count == 0));
451+
},
452+
$"When calling {nameof(MvcController.FullObjectResultAction)} " +
453+
$"action in {nameof(MvcController)} expected the ObjectResult to pass the given predicate, but it failed.");
454+
}
455+
456+
[Fact]
457+
public void PassingShouldCorrectlyRunItsAssertionAction()
458+
{
459+
MyController<MvcController>
460+
.Instance()
461+
.Calling(c => c.FullObjectResultAction())
462+
.ShouldReturn()
463+
.Object(obj => obj
464+
.Passing(o =>
465+
{
466+
const int expectedFormattersCount = 2;
467+
var actualFormattersCount = o.Formatters?.Count;
468+
if (actualFormattersCount != expectedFormattersCount)
469+
{
470+
throw new InvalidAssertionException(
471+
string.Format("Expected {0} to have {1} {2}, but it has {3}.",
472+
o.GetType().ToFriendlyTypeName(),
473+
expectedFormattersCount,
474+
nameof(o.Formatters),
475+
actualFormattersCount));
476+
};
477+
}));
478+
}
425479
}
426480
}

0 commit comments

Comments
 (0)