public void VoidMethodWithInvokes( IFoo foo, Action action1, Action action2) { "Given a fake" .x(() => foo = A.Fake<IFoo>()); "And two delegates" .x(() => { action1 = A.Fake<Action>(); action2 = A.Fake<Action>(); }); "When a void method is configured to invoke a delegate once then invoke another delegate" .x(() => A.CallTo(() => foo.Bar()).Invokes(action1).DoesNothing().Once() .Then.Invokes(action2)); "And the method is called 3 times" .x(() => { foo.Bar(); foo.Bar(); foo.Bar(); }); "Then the first delegate is invoked once, and the second delegate is invoked twice" .x(() => A.CallTo(() => action1()).MustHaveHappened(Repeated.Exactly.Once) .Then(A.CallTo(() => action2()).MustHaveHappened(Repeated.Exactly.Twice))); }
public void VoidMethodWithThrows( IFoo foo, Action action, Exception exception) { "Given a fake" .x(() => foo = A.Fake<IFoo>()); "And an action" .x(() => action = A.Fake<Action>()); "When a void method is configured to invoke a delegate twice then throw an exception" .x(() => A.CallTo(() => foo.Bar()).Invokes(action).DoesNothing().Twice() .Then.Throws<InvalidOperationException>()); "And the method is called 3 times" .x(() => { foo.Bar(); foo.Bar(); exception = Record.Exception(() => foo.Bar()); }); "Then the delegate is invoked twice" .x(() => A.CallTo(() => action()).MustHaveHappened(Repeated.Exactly.Twice)); "And the third call throws an exception" .x(() => exception.Should().BeAnExceptionOfType<InvalidOperationException>()); }
public static void WithNonVoidReturnType(IFoo fake) { "Given a fake with a generic method" .x(() => fake = A.Fake <IFoo>()); "When the fake's generic method is configured to return null for any non-void return type" .x(() => A.CallTo(fake).Where(call => call.Method.Name == "Bar").WithNonVoidReturnType().Returns(null)); "Then the configured method returns null when called with generic argument String" .x(() => fake.Bar <string>().Should().BeNull()); "And the configured method returns null when called with generic argument IFoo" .x(() => fake.Bar <IFoo>().Should().BeNull()); }
public static void AnAppleThatNonWithMatchingArgument(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a method of the fake configured with the An<Apple>.That argument constraint" .x(() => A.CallTo(() => fake.Bar(An <Apple> .That.Matches(a => a.Color == "Red"))).Returns(42)); "When the configured method is called with an argument that doesn't match the constraint" .x(() => result = fake.Bar(new Apple("Green"))); "Then it returns the default value" .x(() => result.Should().Be(0)); }
public void ExceptionInCallbackIsNotWrapped(IFoo fake, Exception exception) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a call to the fake is configured with a callback that throws an exception" .x(() => A.CallTo(() => fake.Bar(0)).Invokes(() => ThrowException())); "When the configured method is called" .x(() => exception = Record.Exception(() => fake.Bar(0))); "Then the original exception should be thrown" .x(() => exception.Should().BeAnExceptionOfType <MyException>().Which.Message.Should().Be("Oops")); }
public static void AnAppleUnderscore(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a method of the fake configured with the An<Apple>._ argument constraint" .x(() => A.CallTo(() => fake.Bar(An <Apple> ._)).Returns(42)); "When the configured method is called" .x(() => result = fake.Bar(new Apple("Red"))); "Then it returns the configured value" .x(() => result.Should().Be(42)); }
public void ExceptionInReturnValueProducerIsNotWrapped(IFoo fake, Exception exception) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a call to the fake is configured with a return value producer that throws an exception" .x(() => A.CallTo(() => fake.Bar(0)).ReturnsLazily(() => ThrowException().GetHashCode())); "When the configured method is called" .x(() => exception = Record.Exception(() => fake.Bar(0))); "Then the original exception should be thrown" .x(() => exception.Should().BeAnExceptionOfType <MyException>().Which.Message.Should().Be("Oops")); }
public static void AnIntThatWithNonMatchingArgument(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a method of the fake configured with the An<int>.That argument constraint" .x(() => A.CallTo(() => fake.Bar(An <int> .That.Matches(i => i % 2 == 1))).Returns(42)); "When the configured method is called with an argument that doesn't match the constraint" .x(() => result = fake.Bar(12)); "Then it returns the default value" .x(() => result.Should().Be(0)); }
public static void AnIntIgnored(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a method of the fake configured with the An<int>.Ignored argument constraint" .x(() => A.CallTo(() => fake.Bar(An <int> .Ignored)).Returns(42)); "When the configured method is called" .x(() => result = fake.Bar(123)); "Then it returns the configured value" .x(() => result.Should().Be(42)); }
public static void WithNonVoidReturnType(IFoo fake) { "Given a fake with a generic method" .x(() => fake = A.Fake<IFoo>()); "When the fake's generic method is configured to return null for any non-void return type" .x(() => A.CallTo(fake).Where(call => call.Method.Name == "Bar").WithNonVoidReturnType().Returns(null)); "Then the configured method returns null when called with generic argument String" .x(() => fake.Bar<string>().Should().BeNull()); "And the configured method returns null when called with generic argument IFoo" .x(() => fake.Bar<IFoo>().Should().BeNull()); }
public static void WhenArgumentsMatchWith2ArgsSuccess(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a fake method with 2 parameters is configured to return a value when the arguments match a predicate" .x(() => A.CallTo(() => fake.Bar(0, string.Empty)) .WhenArgumentsMatch(args => args.Get <int>(0) % 2 == 0 && args.Get <string>(1)?.Length < 3) .Returns(42)); "When the method is called with arguments that satisfy the predicate" .x(() => result = fake.Bar(4, "x")); "Then it returns the configured value" .x(() => result.Should().Be(42)); }
public static void Callback( IFoo fake, bool wasCalled) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And I configure a method to invoke an action" .x(() => A.CallTo(() => fake.Bar()).Invokes(x => wasCalled = true)); "When I call the method" .x(() => fake.Bar()); "Then it invokes the action" .x(() => wasCalled.Should().BeTrue()); }
public static void CallbackOnVoid( IFoo fake, bool wasCalled) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And I configure a method to invoke an action when a void method is called" .x(() => A.CallTo(() => fake.Bar()).Invokes(x => wasCalled = true)); "When I call the method" .x(() => fake.Bar()); "Then it invokes the action" .x(() => wasCalled.Should().BeTrue()); }
public string MethodToBeTested() { string val1, val2; _foo.Bar(ref Errors, out val1, out val2); return(val1 + " " + val2); }
public static void InvokesAfterStrictVoidDoesNotThrow( IFoo fake, Exception exception) { "Given a strict fake" .x(() => fake = A.Fake <IFoo>(options => options.Strict())); "And I configure a void method to invoke an action" .x(() => A.CallTo(() => fake.Bar()).Invokes(() => { })); "When I call the method" .x(() => exception = Record.Exception(() => fake.Bar())); "Then it does not throw an exception" .x(() => exception.Should().BeNull()); }
public static void WithReturnType( IFoo fake, string returnValue) { "Given a fake with a generic method" .x(() => fake = A.Fake<IFoo>()); "When the fake's generic method is configured to return a specific value for a given return type" .x(() => A.CallTo(fake).Where(call => call.Method.Name == "Bar").WithReturnType<string>().Returns(returnValue = "hello world")); "Then the configured method returns the specified value when called with generic argument String" .x(() => fake.Bar<string>().Should().Be(returnValue)); "And the configured method returns a dummy when called with generic argument IFoo" .x(() => fake.Bar<IFoo>().Should().NotBeNull().And.BeAssignableTo<IFoo>()); }
public static void StronglyTypedWhenArgumentsMatchWith4ArgsFailure(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a fake method with 4 parameters is configured to return a value when the arguments match a predicate" .x(() => A.CallTo(() => fake.Bar(0, null, false, null)) .WhenArgumentsMatch((int a, string b, bool c, object d) => a % 2 == 0 && b.Length < 3 && c && d != null) .Returns(42)); "When the method is called with arguments that don't satisfy the predicate" .x(() => result = fake.Bar(3, "hello", false, null)); "Then it returns the default value" .x(() => result.Should().Be(0)); }
public static void UnusedVoidCallSpec( IFoo fake, Exception exception) { "Given a strict fake" .x(() => fake = A.Fake <IFoo>(o => o.Strict())); "When I specify a call to a void method without configuring its behavior" .x(() => A.CallTo(() => fake.Bar())); "And I make a call to that method" .x(() => exception = Record.Exception(() => fake.Bar())); "Then it throws an expectation exception" .x(() => exception.Should().BeAnExceptionOfType <ExpectationException>()); }
public static void WhenArgumentsMatchWith1ArgFailure(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a fake method with 1 parameter is configured to return a value when the arguments match a predicate" .x(() => A.CallTo(() => fake.Bar(0)) .WhenArgumentsMatch(args => args.Get <int>(0) % 2 == 0) .Returns(42)); "When the method is called with an argument that doesn't satisfy the predicate" .x(() => result = fake.Bar(3)); "Then it returns the default value" .x(() => result.Should().Be(0)); }
public static void StronglyTypedWhenArgumentsMatchWith1ArgSuccess(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a fake method with 1 parameter is configured to return a value when the arguments match a predicate" .x(() => A.CallTo(() => fake.Bar(0)) .WhenArgumentsMatch((int a) => a % 2 == 0) .Returns(42)); "When the method is called with an argument that satisfies the predicate" .x(() => result = fake.Bar(4)); "Then it returns the configured value" .x(() => result.Should().Be(42)); }
public static void WhenArgumentsMatchWith2ArgsFailure(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a fake method with 2 parameters is configured to return a value when the arguments match a predicate" .x(() => A.CallTo(() => fake.Bar(0, null)) .WhenArgumentsMatch(args => args.Get <int>(0) % 2 == 0 && args.Get <string>(1).Length < 3) .Returns(42)); "When the method is called with arguments that don't satisfy the predicate" .x(() => result = fake.Bar(3, "hello")); "Then it returns the default value" .x(() => result.Should().Be(0)); }
public static void WithAnyArguments(int argument, IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a fake method with 1 parameter is configured to return a value when called with any arguments" .x(() => A.CallTo(() => fake.Bar(0)) .WithAnyArguments() .Returns(42)); $"When the method is called with argument {argument}" .x(() => result = fake.Bar(argument)); "Then it returns the configured value" .x(() => result.Should().Be(42)); }
public static void WithReturnType( IFoo fake, string returnValue) { "Given a fake with a generic method" .x(() => fake = A.Fake <IFoo>()); "When the fake's generic method is configured to return a specific value for a given return type" .x(() => A.CallTo(fake).Where(call => call.Method.Name == "Bar").WithReturnType <string>().Returns(returnValue = "hello world")); "Then the configured method returns the specified value when called with generic argument String" .x(() => fake.Bar <string>().Should().Be(returnValue)); "And the configured method returns a dummy when called with generic argument IFoo" .x(() => fake.Bar <IFoo>().Should().NotBeNull().And.BeAssignableTo <IFoo>()); }
public void DummyUsage() { string arg = "Wololo"; Console.WriteLine(_foo.Bar(arg)); Console.WriteLine(_constFoo.Bar()); }
public static void StronglyTypedWhenArgumentsMatchWith3ArgsSuccess(IFoo fake, int result) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a fake method with 3 parameters is configured to return a value when the arguments match a predicate" .x(() => A.CallTo(() => fake.Bar(0, null, false)) .WhenArgumentsMatch((int a, string b, bool c) => a % 2 == 0 && b.Length < 3 && c) .Returns(42)); "When the method is called with arguments that satisfy the predicate" .x(() => result = fake.Bar(4, "x", true)); "Then it returns the configured value" .x(() => result.Should().Be(42)); }
public ActionResult GetZZ() { ApplyResponseHeaders(); var result = new JsonResult(); MyFunction(_foo.Bar(), result); // use dependency return(result); }
public static void Callback( IFoo fake, bool wasCalled) { "establish" .x(() => fake = A.Fake<IFoo>()); "when configuring callback" .x(() => { A.CallTo(() => fake.Bar()).Invokes(x => wasCalled = true); fake.Bar(); }); "it should invoke the callback" .x(() => wasCalled.Should().BeTrue()); }
public static void Callback( IFoo fake, bool wasCalled) { "establish" .x(() => fake = A.Fake <IFoo>()); "when configuring callback" .x(() => { A.CallTo(() => fake.Bar()).Invokes(x => wasCalled = true); fake.Bar(); }); "it should invoke the callback" .x(() => wasCalled.Should().BeTrue()); }
public void Should_do_with_argument_as_directed_and_if_pain_persists_see_your_coder() { string stringArg = null; _sub.Bar(Arg.Do <string>(x => stringArg = x), 1, _someObject); _sub.Bar("hello world", 1, _someObject); Assert.That(stringArg, Is.EqualTo("hello world")); }
public static void OrderedAssertionsUsingRepeatedInOrder(IFoo fake, Exception exception) { "Given a Fake" .x(() => fake = A.Fake <IFoo>()); "And a call on the Fake, passing argument 1" .x(() => fake.Bar(1)); "And a call on the Fake, passing argument 1" .x(() => fake.Bar(1)); "And a call on the Fake, passing argument 2" .x(() => fake.Bar(2)); "And a call on the Fake, passing argument 3" .x(() => fake.Bar(3)); "When I use Repeated to assert that a call with argument 1 was made twice exactly, then a call with argument 2, and then a call with argument 3" .x(() => exception = Record.Exception(() => A.CallTo(() => fake.Bar(1)).MustHaveHappened(Repeated.Exactly.Twice) .Then(A.CallTo(() => fake.Bar(2)).MustHaveHappened()) .Then(A.CallTo(() => fake.Bar(3)).MustHaveHappened()))); "Then the assertion should pass" .x(() => exception.Should().BeNull()); }
public static void MultistepOrderedAssertionsInOrder( IFoo fake, Exception exception, IOrderableCallAssertion lastAssertion) { "Given a Fake" .x(() => fake = A.Fake<IFoo>()); "And a call on the Fake, passing argument 1" .x(() => fake.Bar(1)); "And a call on the Fake, passing argument 2" .x(() => fake.Bar(2)); "And a call on the Fake, passing argument 2" .x(() => fake.Bar(2)); "And a call on the Fake, passing argument 3" .x(() => fake.Bar(3)); "When I assert that a call with argument 1 was made exactly once" .x(() => lastAssertion = A.CallTo(() => fake.Bar(1)).MustHaveHappened(Repeated.Exactly.Once)); "And then a call with argument 2" .x(() => lastAssertion = lastAssertion.Then(A.CallTo(() => fake.Bar(2)).MustHaveHappened())); "And then a call with argument 3 exactly once" .x(() => exception = Record.Exception(() => lastAssertion.Then(A.CallTo(() => fake.Bar(3)).MustHaveHappened(Repeated.Exactly.Once)))); "Then the assertions should pass" .x(() => exception.Should().BeNull()); }
public void Unexpected_call_was_received() { _foo.Bar(); ShouldThrow(() => _foo.DidNotReceive().Bar()); ShouldThrow(() => _foo.Received(0).Bar()); }
public static void StronglyTypedWhenArgumentsMatchWith1ArgWrongSignature(IFoo fake, Exception exception) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a fake method with 1 parameter is configured with an arguments predicate with an incompatible signature" .x(() => exception = Record.Exception( () => A.CallTo(() => fake.Bar(0)) .WhenArgumentsMatch((long a) => true) .Returns(42))); "When the method is called" .x(() => exception = Record.Exception(() => fake.Bar(3))); "Then it throws a FakeConfigurationException that describes the problem" .x(() => exception.Should().BeAnExceptionOfType <FakeConfigurationException>() .WithMessage("The faked method has the signature (System.Int32), but when arguments match was used with (System.Int64).")); }
public void ExceptionInExceptionFactoryIsNotWrapped(IFoo fake, Exception exception) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a call to the fake is configured with an exception factory that throws an exception" .x(() => A.CallTo(() => fake.Bar(0)).Throws(call => { ThrowException(); return(new Exception("test")); })); "When the configured method is called" .x(() => exception = Record.Exception(() => fake.Bar(0))); "Then the original exception should be thrown" .x(() => exception.Should().BeAnExceptionOfType <MyException>().Which.Message.Should().Be("Oops")); }
public void ActualCallDescriptionForMethod(IFoo fake, Exception exception) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And I make a call to Bar with argument 0" .x(() => fake.Bar(42)); "When I assert that the Bar method was called with argument 42" .x(() => exception = Record.Exception(() => A.CallTo(() => fake.Bar(0)).MustHaveHappened())); "Then the assertion should fail" .x(() => exception.Should().BeAnExceptionOfType <ExpectationException>()); "And the exception should correctly describe the actual call that was made" .x(() => exception.Message.Should().MatchModuloLineEndings( "*\r\n 1: FakeItEasy.Specs.CallDescriptionsInAssertionSpecs+IFoo.Bar(i: 42)\r\n*")); }
public static void StronglyTypedWhenArgumentsMatchWith4ArgsWrongSignature(IFoo fake, Exception exception) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a fake method with 4 parameters is configured with an arguments predicate with an incompatible signature" .x(() => exception = Record.Exception( () => A.CallTo(() => fake.Bar(0, null, false, null)) .WhenArgumentsMatch((long a, DateTime b, Type c, char d) => true) .Returns(42))); "When the method is called" .x(() => exception = Record.Exception(() => fake.Bar(3, "hello", true, new object()))); "Then it throws a FakeConfigurationException that describes the problem" .x(() => exception.Should().BeAnExceptionOfType <FakeConfigurationException>() .WithMessage("The faked method has the signature (System.Int32, System.String, System.Boolean, System.Object), but when arguments match was used with (System.Int64, System.DateTime, System.Type, System.Char).")); }
public void RequestUnsecuredReply() { CustomBinding b = CreateBinding(delegate(Message input) { return(input); }); IFoo foo = ChannelFactory <IFoo> .CreateChannel(b, CreateX509EndpointAddress ("stream:dummy")); foo.Bar(Message.CreateMessage(b.MessageVersion, "http://tempuri.org/IFoo/Bar")); }
public void NonCanceledTokenWithConfiguredCall( IFoo fake, CancellationToken cancellationToken, int result) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And a call configured on that fake" .x(() => A.CallTo(() => fake.Bar(A<CancellationToken>._)).Returns(42)); "And a cancellation token that is not canceled" .x(() => cancellationToken = new CancellationToken(false)); "When the configured method is called with this cancellation token" .x(() => result = fake.Bar(cancellationToken)); "Then it doesn't throw and returns the configured value" .x(() => result.Should().Be(42)); }
public void NonCanceledToken( IFoo fake, CancellationToken cancellationToken, int result) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And a cancellation token that is not canceled" .x(() => cancellationToken = new CancellationToken(false)); "When a method is called with this cancellation token" .x(() => result = fake.Bar(cancellationToken)); "Then it doesn't throw and returns the default value" .x(() => result.Should().Be(0)); }
public void CanceledToken( IFoo fake, CancellationToken cancellationToken, Exception exception) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And a cancellation token that is canceled" .x(() => cancellationToken = new CancellationToken(true)); "When a method is called with this cancellation token" .x(() => exception = Record.Exception(() => fake.Bar(cancellationToken))); "Then it throws an OperationCanceledException" .x(() => exception.Should().BeAnExceptionAssignableTo<OperationCanceledException>()); }
public void Test4(IFoo foo) { foo.Bar(); }
public static void OrderedAssertionsInOrder(IFoo fake, Exception exception) { "Given a Fake" .x(() => fake = A.Fake<IFoo>()); "And a call on the Fake, passing argument 1" .x(() => fake.Bar(1)); "And a call on the Fake, passing argument 1" .x(() => fake.Bar(1)); "And a call on the Fake, passing argument 2" .x(() => fake.Bar(2)); "And a call on the Fake, passing argument 3" .x(() => fake.Bar(3)); "When I assert that a call with argument 1 was made exactly twice, then a call with argument 2, and then a call with argument 3" .x(() => exception = Record.Exception(() => A.CallTo(() => fake.Bar(1)).MustHaveHappened(Repeated.Exactly.Twice) .Then(A.CallTo(() => fake.Bar(2)).MustHaveHappened()) .Then(A.CallTo(() => fake.Bar(3)).MustHaveHappened()))); "Then the assertion should pass" .x(() => exception.Should().BeNull()); }
public static void OrderedAssertionsOnDifferentObjectsInOrder(IFoo fake1, IFoo fake2, Exception exception) { "Given a Fake" .x(() => fake1 = A.Fake<IFoo>()); "And another Fake of the same type" .x(() => fake2 = A.Fake<IFoo>()); "And a call on the first Fake, passing argument 1" .x(() => fake1.Bar(1)); "And a call on the second Fake, passing argument 1" .x(() => fake2.Bar(1)); "And a call on the first Fake, passing argument 2" .x(() => fake1.Bar(2)); "When I assert that a call with argument 1 was made on the first Fake, then on the second, and then that a call with argument 2 was made on the first Fake" .x(() => exception = Record.Exception(() => A.CallTo(() => fake1.Bar(1)).MustHaveHappened() .Then(A.CallTo(() => fake2.Bar(1)).MustHaveHappened()) .Then(A.CallTo(() => fake1.Bar(2)).MustHaveHappened()))); "Then the assertion should pass" .x(() => exception.Should().BeNull()); }
public static void OrderedAssertionsOnDifferentObjectsOutOfOrder(IFoo fake1, IFoo fake2, Exception exception) { "Given a Fake" .x(() => fake1 = A.Fake<IFoo>()); "And another Fake of the same type" .x(() => fake2 = A.Fake<IFoo>()); "And a call on the second Fake, passing argument 1" .x(() => fake2.Bar(1)); "And a call on the first Fake, passing argument 1" .x(() => fake1.Bar(1)); "And a call on the first Fake, passing argument 2" .x(() => fake1.Bar(2)); "When I assert that a call with argument 1 was made on the first Fake, then on the second, and then that a call with argument 2 was made on the first Fake" .x(() => exception = Record.Exception(() => A.CallTo(() => fake1.Bar(1)).MustHaveHappened() .Then(A.CallTo(() => fake2.Bar(1)).MustHaveHappened()) .Then(A.CallTo(() => fake1.Bar(2)).MustHaveHappened()))); "Then the assertion should fail" .x(() => exception.Should().BeAnExceptionOfType<ExpectationException>().WithMessage(@" Assertion failed for the following calls: 'FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(1)' repeated at least once 'FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(1)' repeated at least once The calls were found but not in the correct order among the calls: 1: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(baz: 1) 2: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(baz: 1) 3: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(baz: 2) ")); }
public static void OrderedAssertionWithRepeatConstraintFailure( IFoo fake, IOrderableCallAssertion lastAssertion, Exception exception) { "Given a Fake" .x(() => fake = A.Fake<IFoo>()); "And a call on the Fake, passing argument 1" .x(() => fake.Bar(1)); "And a call on the Fake, passing argument 2" .x(() => fake.Bar(2)); "And a call on the Fake, passing argument 1" .x(() => fake.Bar(1)); "When I assert that a call with argument 2 was made" .x(() => lastAssertion = A.CallTo(() => fake.Bar(2)).MustHaveHappened()); "And then that a call with argument 1 was made exactly once" .x(() => exception = Record.Exception(() => lastAssertion.Then(A.CallTo(() => fake.Bar(1)).MustHaveHappened(Repeated.Exactly.Once)))); "Then the last assertion should fail" .x(() => exception.Should().BeAnExceptionOfType<ExpectationException>()); "And the message should say that the call to Bar(1) was found too many times" .x(() => exception.Message.Should().Be(@" Assertion failed for the following call: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(1) Expected to find it exactly once but found it #2 times among the calls: 1: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(baz: 1) 2: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(baz: 2) 3: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(baz: 1) ")); }
public static void UnusedVoidCallSpec( IFoo fake, Exception exception) { "Given a strict fake" .x(() => fake = A.Fake<IFoo>(o => o.Strict())); "When I specify a call to a void method without configuring its behavior" .x(() => A.CallTo(() => fake.Bar())); "And I make a call to that method" .x(() => exception = Record.Exception(() => fake.Bar())); "Then it throws an expectation exception" .x(() => exception.Should().BeAnExceptionOfType<ExpectationException>()); }
public static void InvokesAfterStrictVoid( IFoo fake, Exception exception) { "Given a strict fake" .x(() => fake = A.Fake<IFoo>(options => options.Strict())); "And I configure a void method to invoke an action" .x(() => A.CallTo(() => fake.Bar()).Invokes(() => { })); "When I call the method" .x(() => exception = Record.Exception(() => fake.Bar())); "Then it does not throw an exception" .x(() => exception.Should().BeNull()); }
public static void DoesNothingAndThrowsAppliedToSameACallTo( IFoo fake, IVoidArgumentValidationConfiguration callToBar, Exception exception) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And I identify a method to configure" .x(() => callToBar = A.CallTo(() => fake.Bar())); "And I configure the method to do nothing" .x(() => callToBar.DoesNothing()); "When I configure the method to throw an exception" .x(() => exception = Record.Exception(() => callToBar.Throws<Exception>())); "Then it throws an invalid operation exception" .x(() => exception.Should().BeAnExceptionOfType<InvalidOperationException>()); }
public static void MultistepOrderedAssertionsOutOfOrder( IFoo fake, Exception exception, IOrderableCallAssertion lastAssertion) { "Given a Fake" .x(() => fake = A.Fake<IFoo>()); "And a call on the Fake, passing argument 3" .x(() => fake.Bar(3)); "And a call on the Fake, passing argument 1" .x(() => fake.Bar(1)); "And a call on the Fake, passing argument 1" .x(() => fake.Bar(1)); "And a call on the Fake, passing argument 2" .x(() => fake.Bar(2)); "When I assert that a call with argument 1 was made exactly twice" .x(() => lastAssertion = A.CallTo(() => fake.Bar(1)).MustHaveHappened(Repeated.Exactly.Twice)); "And then a call with argument 2" .x(() => lastAssertion = lastAssertion.Then(A.CallTo(() => fake.Bar(2)).MustHaveHappened())); "And then that a call with argument 3 was made exactly once" .x(() => exception = Record.Exception(() => lastAssertion.Then(A.CallTo(() => fake.Bar(3)).MustHaveHappened(Repeated.Exactly.Once)))); "Then the last assertion should fail" .x(() => exception.Should().BeAnExceptionOfType<ExpectationException>().WithMessage(@" Assertion failed for the following calls: 'FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(1)' repeated exactly twice 'FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(2)' repeated at least once 'FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(3)' repeated exactly once The calls were found but not in the correct order among the calls: 1: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(baz: 3) 2: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(baz: 1) repeated 2 times ... 4: FakeItEasy.Specs.OrderedCallMatchingSpecs+IFoo.Bar(baz: 2)")); }
public void Configure(IApplicationBuilder app, IFoo foo) { foo.Bar(); }
public void AnyCallWithInvokesAndThrows( IFoo foo, Action action1, Action action2, Exception exception) { "Given a fake" .x(() => foo = A.Fake<IFoo>()); "And two delegates" .x(() => { action1 = A.Fake<Action>(); action2 = A.Fake<Action>(); }); "When any method is configured to invoke a delegate once, then another delegate twice, then throw an exception" .x(() => A.CallTo(foo).Invokes(action1).DoesNothing().Once() .Then.Invokes(action2).DoesNothing().Twice() .Then.Throws<InvalidOperationException>()); "And 4 calls are made on the fake" .x(() => { foo.Bar(); foo.Bar(1); foo.Bar(2); exception = Record.Exception(() => foo.Bar(3)); }); "Then the first delegate is invoked once, and the second delegate is invoked twice" .x(() => A.CallTo(() => action1()).MustHaveHappened(Repeated.Exactly.Once) .Then(A.CallTo(() => action2()).MustHaveHappened(Repeated.Exactly.Twice))); "And the fourth call throws an exception" .x(() => exception.Should().BeAnExceptionOfType<InvalidOperationException>()); }
public void CanceledTokenWithConfiguredCallForNonCanceledToken( IFoo fake, CancellationToken cancellationToken, Exception exception) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And a call configured on that fake for a non-canceled token" .x(() => A.CallTo(() => fake.Bar(A<CancellationToken>.That.IsNotCanceled())).Returns(42)); "And a cancellation token that is canceled" .x(() => cancellationToken = new CancellationToken(true)); "When the configured method is called with this cancellation token" .x(() => exception = Record.Exception(() => fake.Bar(cancellationToken))); "Then it throws an OperationCanceledException" .x(() => exception.Should().BeAnExceptionAssignableTo<OperationCanceledException>()); }
public void CanceledTokenPassedAsObject( IFoo fake, CancellationToken cancellationToken, int result) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And a cancellation token that is canceled" .x(() => cancellationToken = new CancellationToken(true)); "When a method is called with this cancellation token passed as Object" .x(() => result = fake.Bar((object)cancellationToken)); "Then it doesn't throw and returns the default value" .x(() => result.Should().Be(0)); }
public void Do(IFoo foo) { foo.Bar(); }