/// <summary> /// Verifies the answer of the given question using a NUnit constraint /// </summary> /// <typeparam name="T">The answer type</typeparam> /// /// <param name="verifies">The <see cref="IVerifies"/> instance</param> /// <param name="question">The question to verify the answer from</param> /// <param name="constraint">A NUnit constraint that encapsulate the expected result</param> /// <param name="message">The message to return when the assertion fails</param> /// <param name="args">Arguments to add to the message</param> /// <returns>The answer, when the verification succeeds</returns> public static T Then <T>( this IVerifies verifies, IQuestion <T> question, IResolveConstraint constraint, string message, params object[] args) { if (verifies == null) { throw new ArgumentNullException(nameof(verifies)); } if (question == null) { throw new ArgumentNullException(nameof(question)); } if (constraint == null) { throw new ArgumentNullException(nameof(constraint)); } if (message == null) { throw new ArgumentNullException(nameof(message)); } if (args == null) { throw new ArgumentNullException(nameof(args)); } return(verifies.Then(question, answer => Assert.That(answer, constraint, message, args))); }
/// <summary> /// Verifies the answer of the given question using a NUnit constraint /// </summary> /// <typeparam name="T">The answer type</typeparam> /// /// <param name="verifies">The <see cref="IVerifies"/> instance</param> /// <param name="question">The question to verify the answer from</param> /// <param name="constraint">A NUnit constraint that encapsulate the expected result</param> /// <param name="getExceptionMessage">A function that returns the message to return when the assertion fails</param> /// <returns>The answer, when the verification succeeds</returns> public static T Then <T>( this IVerifies verifies, IQuestion <T> question, IResolveConstraint constraint, Func <string> getExceptionMessage) { if (verifies == null) { throw new ArgumentNullException(nameof(verifies)); } if (question == null) { throw new ArgumentNullException(nameof(question)); } if (constraint == null) { throw new ArgumentNullException(nameof(constraint)); } if (getExceptionMessage == null) { throw new ArgumentNullException(nameof(getExceptionMessage)); } return(verifies.Then(question, answer => Assert.That(answer, constraint, getExceptionMessage))); }
/// <summary> /// Verifies the answer of the given question using a NUnit constraint /// </summary> /// <typeparam name="T">The answer type</typeparam> /// /// <param name="verifies">The <see cref="IVerifies"/> instance</param> /// <param name="question">The question to verify the answer from</param> /// <param name="constraint">A NUnit constraint that encapsulate the expected result</param> /// <param name="message">The message to return when the assertion fails</param> /// <param name="args">Arguments to add to the message</param> /// <returns>The answer, when the verification succeeds</returns> public static T Then <T>( this IVerifies verifies, IQuestion <T> question, IResolveConstraint constraint, string message, params object[] args) { ValidateArguments(verifies, question, constraint, message, args); return(verifies.Then(question, answer => Assert.That(answer, constraint, message, args))); }
/// <summary> /// Verifies the answer of the given question using a NUnit constraint /// </summary> /// <typeparam name="T">The answer type</typeparam> /// /// <param name="verifies">The <see cref="IVerifies"/> instance</param> /// <param name="question">The question to verify the answer from</param> /// <param name="constraint">A NUnit constraint that encapsulate the expected result</param> /// <returns>The answer, when the verification succeeds</returns> public static Task <T> Then <T>(this IVerifies verifies, IQuestion <Task <T> > question, IResolveConstraint constraint) { if (verifies == null) { throw new ArgumentNullException(nameof(verifies)); } if (question == null) { throw new ArgumentNullException(nameof(question)); } if (constraint == null) { throw new ArgumentNullException(nameof(constraint)); } return(verifies.Then <T>(question, answer => Assert.That(answer, constraint))); }
public void Then_ShouldCallAction( IVerifies verifies, IActor actor, object expected) { // arrange var verification = new Mock <Action <object> >(); var question = Questions.FromResult(expected); Mock.Get(verifies).Setup(a => a.Then(It.IsAny <IQuestion <object> >(), It.IsAny <Func <object, object> >())) .Returns((IQuestion <object> q, Func <object, object> f) => { return(f(q.AnsweredBy(actor))); }); // act var actual = verifies.Then(question, verification.Object); // assert Assert.Equal(expected, actual); verification.Verify(v => v(expected)); }
/// <summary> /// Verifies the answer of the given question /// </summary> /// <typeparam name="TAnswer">The answer type</typeparam> /// <param name="actor">The actor</param> /// <param name="question">The question to verify the answer from</param> /// <param name="verifyAction">The action that verifies the answer. This action can throw assertion exceptions (using the Assert of a unit test framework) to indicates that the verification fails.</param> /// <returns>The answer, when the verification succeeds</returns> public static TAnswer Then <TAnswer>(this IVerifies actor, IQuestion <TAnswer> question, Action <TAnswer> verifyAction) { if (actor is null) { throw new ArgumentNullException(nameof(actor)); } if (question is null) { throw new ArgumentNullException(nameof(question)); } if (verifyAction is null) { throw new ArgumentNullException(nameof(verifyAction)); } return(actor.Then(question, result => { verifyAction(result); return result; })); }
public async Task Then_Async_ShouldExecuteAction( IVerifies verifies, IActor actor, object expected) { // arrange var mockAction = new Mock <Action <object> >(); var task = Task.FromResult(expected); var question = Questions.FromResult(task); Mock.Get(verifies).Setup(a => a.Then(It.IsAny <IQuestion <Task <object> > >(), It.IsAny <Func <Task <object>, Task <object> > >())) .Returns((IQuestion <Task <object> > q, Func <Task <object>, Task <object> > f) => { return(f(q.AnsweredBy(actor))); }); // act var actual = await verifies.Then(question, mockAction.Object); // assert Assert.Equal(expected, actual); mockAction.Verify(a => a(expected)); }
/// <summary> /// Verifies the answer of the given asynchronous question /// </summary> /// <typeparam name="TAnswer">The answer type</typeparam> /// <param name="actor">The actor</param> /// <param name="question">The question to verify the answer from</param> /// <param name="verifyAction">The action that verifies the answer. This action can throw assertion exceptions (using the Assert of a unit test framework) to indicates that the verification fails.</param> /// <returns>The answer, when the verification succeeds</returns> public static Task <TAnswer> Then <TAnswer>(this IVerifies actor, IQuestion <Task <TAnswer> > question, Action <TAnswer> verifyAction) { if (actor is null) { throw new ArgumentNullException(nameof(actor)); } if (question is null) { throw new ArgumentNullException(nameof(question)); } if (verifyAction is null) { throw new ArgumentNullException(nameof(verifyAction)); } return(actor.Then(question, async resultTask => { var result = await resultTask; verifyAction(result); return result; })); }