private static IWritableFakeObjectCall CreateFakeCall <TFake>(TFake fakedObject, LambdaExpression callSpecification) { var result = A.Fake <IWritableFakeObjectCall>(); var frozen = A.Fake <ICompletedFakeObjectCall>(); Fake.Configure(result) .CallsTo(x => x.Method).Returns(GetMethodInfo(callSpecification)); Fake.Configure(frozen) .CallsTo(x => x.Method).Returns(GetMethodInfo(callSpecification)); Fake.Configure(result) .CallsTo(x => x.FakedObject).Returns(fakedObject); Fake.Configure(frozen) .CallsTo(x => x.FakedObject).Returns(fakedObject); Fake.Configure(result) .CallsTo(x => x.Arguments).Returns(CreateArgumentCollection(fakedObject, callSpecification)); Fake.Configure(frozen) .CallsTo(x => x.Arguments).Returns(CreateArgumentCollection(fakedObject, callSpecification)); Fake.Configure(frozen) .CallsTo(x => x.ReturnValue) .Returns(() => Fake.GetCalls(result).Matching(x => x.SetReturnValue(Argument.Is.Any <object>())).Last().Arguments[0]); Fake.Configure(result) .CallsTo(x => x.Freeze()) .Returns(frozen); return(result); }
public void Matches_should_use_argument_validators_to_validate_each_argument_of_call() { var argument1 = "foo"; var argument2 = 10; var validator = A.Fake <IArgumentValidator>(); Fake.Configure(validator) .CallsTo(x => x.IsValid(Argument.Is.Any <object>())) .Returns(true); Fake.Configure(this.validatorFactory) .CallsTo(x => x.GetArgumentValidator(Argument.Is.Any <Expression>())) .Returns(validator); var call = ExpressionHelper.CreateFakeCall <IFoo>(x => x.Bar(argument1, argument2)); var matcher = this.CreateMatcher <IFoo>(x => x.Bar(argument1, argument2)); matcher.Matches(call); Fake.Assert(validator) .WasCalled(x => x.IsValid(argument1)); Fake.Assert(validator) .WasCalled(x => x.IsValid(argument2)); }
public void Exception_message_should_skip_duplicate_calls_in_row() { this.StubCalls(10); foreach (var call in this.calls) { Fake.Configure(call) .CallsTo(x => x.ToString()) .Returns("Fake call"); } Fake.Configure(this.calls[9]) .CallsTo(x => x.ToString()) .Returns("Other call"); var asserter = this.CreateAsserter(); var message = this.GetExceptionMessage(() => asserter.AssertWasCalled(x => false, "", x => false, "")); Assert.That(message, Text.Contains(@" 1. 'Fake call' repeated 9 times ... 10. 'Other call'")); }
public void AssertWasCalled_with_function_call_calls_WasCalled_on_assertions_from_factory() { var fake = new Fake <IFoo>(); var fakeObject = Fake.GetFakeObject(fake.FakedObject); var factoryAssertions = A.Fake <IFakeAssertions <IFoo> >(); Expression <Func <IFoo, int> > callSpecification = x => x.Baz(); using (Fake.CreateScope()) { var factory = this.StubResolveWithFake <IFakeAssertionsFactory>(); Fake.Configure(factory) .CallsTo(x => x.CreateAsserter <IFoo>(fakeObject)) .Returns(factoryAssertions); fake.AssertWasCalled(callSpecification); } foreach (var call in Fake.GetCalls(factoryAssertions)) { Console.WriteLine(call); } Fake.Assert(factoryAssertions) .WasCalled(x => x.WasCalled(callSpecification)); }
public void Exception_message_should_not_skip_duplicate_messages_that_are_not_in_row() { this.StubCalls(4); using (var callsEnumerator = this.calls.GetEnumerator()) { while (callsEnumerator.MoveNext()) { Fake.Configure(callsEnumerator.Current) .CallsTo(x => x.ToString()) .Returns("odd"); if (callsEnumerator.MoveNext()) { Fake.Configure(callsEnumerator.Current) .CallsTo(x => x.ToString()) .Returns("even"); } } } var asserter = this.CreateAsserter(); var message = this.GetExceptionMessage(() => asserter.AssertWasCalled(x => false, "", x => false, "")); Assert.That(message, Text.Contains(@" 1. 'odd' 2. 'even' 3. 'odd' 4. 'even' ")); }
public void Exception_message_should_list_the_calls_in_the_calls_collection() { this.StubCalls(10); int callNumber = 1; foreach (var call in this.calls) { var boundCallNumber = callNumber; Fake.Configure(call) .CallsTo(x => x.ToString()) .Returns(x => "Fake call " + boundCallNumber.ToString()); callNumber++; } var asserter = this.CreateAsserter(); var message = this.GetExceptionMessage(() => asserter.AssertWasCalled(x => false, "", x => false, "")); Assert.That(message, Text.Contains(@" 1. 'Fake call 1' 2. 'Fake call 2' 3. 'Fake call 3' 4. 'Fake call 4' 5. 'Fake call 5' 6. 'Fake call 6' 7. 'Fake call 7' 8. 'Fake call 8' 9. 'Fake call 9' 10. 'Fake call 10'")); }
public void SetUp() { this.validatorFactory = A.Fake <ArgumentValidatorFactory>(); var validator = A.Fake <IArgumentValidator>(); Fake.Configure(validator).CallsTo(x => x.IsValid(Argument.Is.Any <object>())).Returns(true); Fake.Configure(validatorFactory).CallsTo(x => x.GetArgumentValidator(Argument.Is.Any <Expression>())).Returns(validator); }
public void Fake_object_is_not_serializable_once_it_has_been_configured() { var foo = A.Fake <IFoo>(); Fake.Configure(foo).CallsTo(x => x.Bar()).Returns("test"); Assert.That(foo, Is.Not.BinarySerializable); }
public void Properties_should_be_set_to_fake_objects_when_property_type_is_fakeable_and_the_property_is_not_explicitly_set() { var foo = A.Fake <IFoo>(); Fake.Configure(foo.ChildFoo).CallsTo(x => x.Baz()).Returns(10); Assert.That(foo.ChildFoo.Baz(), Is.EqualTo(10)); }
public void Configure_should_return_fake_configuration_object() { var fake = A.Fake <IFoo>(); var configuration = (FakeConfiguration <IFoo>)Fake.Configure(fake); Assert.That(configuration.Fake.Object, Is.SameAs(fake)); }
public void Configure_should_return_fake_configuration_when_called_on_fake_object() { var foo = A.Fake <IFoo>(); var configuration = (FakeConfiguration <IFoo>)Fake.Configure(foo); Assert.That(configuration.Fake, Is.EqualTo(Fake.GetFakeObject(foo))); }
public void Fake_called_with_instance_returns_wrapping_fake_that_delegates_to_wrapped_object() { var wrapped = A.Fake <IFoo>(); var wrapper = A.Fake(wrapped); Fake.Configure(wrapped).CallsTo(x => x.Baz()).Returns(10); Assert.That(wrapper.Baz(), Is.EqualTo(10)); }
private static IFakeObjectCall CreateFakeCall <TFake>(TFake fakedObject, LambdaExpression callSpecification) { var result = A.Fake <IFakeObjectCall>(); Fake.Configure(result).CallsTo(x => x.Method).Returns(GetMethodInfo(callSpecification)); Fake.Configure(result).CallsTo(x => x.FakedObject).Returns(fakedObject); Fake.Configure(result).CallsTo(x => x.Arguments).Returns(CreateArgumentCollection(fakedObject, callSpecification)); return(result); }
public void CreateFake_object_is_not_serializable_once_it_has_been_configured() { var factory = this.CreateFactory(); var foo = factory.CreateFake(typeof(IFoo), null, false) as IFoo; Fake.Configure(foo).CallsTo(x => x.Bar()).Returns("test"); Assert.That(foo, Is.Not.BinarySerializable); }
public void Fake_with_wrapped_instance_will_override_behavior_of_wrapped_object_on_configured_methods() { var wrapped = A.Fake <IFoo>(); var wrapper = A.Fake(wrapped); Fake.Configure(wrapped).CallsTo(x => x.Bar()).Returns("wrapped"); Fake.Configure(wrapper).CallsTo(x => x.Bar()).Returns("wrapper"); Assert.That(wrapper.Bar(), Is.EqualTo("wrapper")); }
public void ToString_should_return_expressionMatcher_ToString() { Fake.Configure(this.callMatcher) .CallsTo(x => x.ToString()) .Returns("foo"); var rule = CreateRule <IFoo>(x => x.Bar()); Assert.That(rule.ToString(), Is.EqualTo("foo")); }
public void ReturnsNull_should_return_configuration_object() { var config = A.Fake <IReturnValueConfiguration <IFoo, string> >(); var returnConfig = A.Fake <IAfterCallSpecifiedConfiguration <IFoo> >(); Fake.Configure(config).CallsTo(x => x.Returns(Argument.Is.Any <string>())).Returns(returnConfig); var returned = config.ReturnsNull(); Assert.That(returned, Is.SameAs(returnConfig)); }
public void DoesNothing_should_set_applicator_that_does_nothing_when_called() { this.StubInterceptorBeingBuilt(); this.configuration.DoesNothing(); var call = A.Fake <IWritableFakeObjectCall>(); Fake.Configure(call).AnyCall().Throws(new AssertionException("Applicator should do nothing.")); this.configuration.RuleBeingBuilt.Applicator(call); }
public static void TraditionalWay() { var foo = A.Fake <IFoo>(); Fake.Configure(foo) .CallsTo(x => x.Baz()).Returns(10); int value = foo.Baz(); Fake.Assert(foo).WasCalled(x => x.Baz()); }
public void WasNotCalled_with_function_call_should_call_asserter_with_matcher_ToString_as_call_description() { Fake.Configure(this.matcher) .CallsTo(x => x.ToString()) .Returns("description"); this.assertions.WasNotCalled(x => x.Biz()); string description = Fake.GetCalls(this.fakeAsserter).Single().Arguments.Get <string>("callDescription"); Assert.That(description, Is.EqualTo("description")); }
public void Generic_Fake_with_constructor_call_should_return_object_from_factory() { var foo = A.Fake <Foo>(); Fake.Configure(this.factory) .CallsTo(x => x.CreateFake(typeof(Foo), Argument.Is.Any <IEnumerable <object> >(), true)) .Returns(foo); var returned = A.Fake <Foo>(() => new Foo(A.Fake <IServiceProvider>())); Assert.That(returned, Is.SameAs(foo)); }
public void Recording_a_rule_should_work_even_when_there_are_already_call_rules_specified() { var fake = A.Fake <IFoo>(); Fake.Configure(fake).CallsTo(x => x.Baz()).Returns(10); Fake.Configure(fake).CallsTo(x => x.Biz()).Returns("something"); ThisCall.To(fake).Throws(new ApplicationException()); fake.Bar(); Assert.Throws <ApplicationException>(() => fake.Bar()); }
public void CreateFake_should_return_fake_from_container_when_non_proxyied_objects_are_allowed_and_container_contains_type() { var factory = this.CreateFactory(); object result = null; Fake.Configure(this.container) .CallsTo(x => x.TryCreateFakeObject(typeof(int), Argument.Is.SameSequence <object>("foo"), out result)) .Returns(true) .AssignsOutAndRefParameters(1); Assert.That(factory.CreateFake(typeof(int), new object[] { "foo" }, true), Is.EqualTo(1)); }
public void SetUp() { this.replacedServiceLocator = ServiceLocator.Current; ServiceLocator.Current = A.Fake <ServiceLocator>(ServiceLocator.Current); this.factory = A.Fake(new FakeObjectFactory(A.Fake <IFakeObjectContainer>())); FakeObjectFactory.Creator creator = () => this.factory; Fake.Configure(ServiceLocator.Current) .CallsTo(x => x.Resolve(typeof(FakeObjectFactory.Creator))) .Returns(creator); }
public void CallBaseMethod_should_call_Proceed_on_invocation() { var invokation = A.Fake <IInvocation>(); Fake.Configure(invokation).CallsTo(x => x.Arguments).Returns(new object[] { }); Fake.Configure(invokation).CallsTo(x => x.Method).Returns(typeof(IFoo).GetMethod("Bar", new Type[] { })); var adapter = new InvocationCallAdapter(invokation); adapter.CallBaseMethod(); Fake.Assert(invokation).WasCalled(x => x.Proceed()); }
public void Constructor_sets_fake_object_returned_from_factory_to_FakedObject_property() { var foo = A.Fake <IFoo>(); using (Fake.CreateScope()) { Fake.Configure(this.factory) .CallsTo(x => x.CreateFake(typeof(IFoo), null, false)).Returns(foo); var fake = new Fake <IFoo>(); Assert.That(fake.FakedObject, Is.SameAs(foo)); } }
public void CreateFake_should_return_fake_from_proxy_generator_when_container_does_not_contain_type() { var factory = this.CreateFactory(); var returned = new TestableProxyResult(typeof(IFoo), (IFakedProxy)A.Fake <IFoo>()); Fake.Configure(this.proxyGenerator) .CallsTo(x => x.TryGenerateProxy(typeof(IFoo), Argument.Is.Any <FakeObject>(), this.container, out Null <ProxyResult> .Out)) .Returns(true) .AssignsOutAndRefParameters(returned); Assert.That(factory.CreateFake(typeof(IFoo), null, true), Is.SameAs(returned.Proxy)); }
public void SetArgumentValue_sets_the_argument_value_of_the_invokation() { var invocation = A.Fake <IInvocation>(); Fake.Configure(invocation).CallsTo(x => x.Arguments).Returns(new object[] { }); Fake.Configure(invocation).CallsTo(x => x.Method).Returns(typeof(IFoo).GetMethod("Bar", new Type[] { })); var adapter = new InvocationCallAdapter(invocation); adapter.SetArgumentValue(0, "test"); Fake.Assert(invocation) .WasCalled(x => x.SetArgumentValue(0, "test")); }
protected override void OnSetUp() { this.fakeAssertions = A.Fake <IFakeAssertions <IFoo> >(); this.fakeAssertionsFactory = A.Fake <IFakeAssertionsFactory>(); Fake.Configure(this.fakeAssertionsFactory) .CallsTo(x => x.CreateAsserter <IFoo>(Argument.Is.Any <FakeObject>())) .Returns(this.fakeAssertions); this.fakeConfiguration = A.Fake <IFakeConfiguration <IFoo> >(); this.fakeConfigurationFactory = A.Fake <IFakeConfigurationFactory>(); Fake.Configure(this.fakeConfigurationFactory) .CallsTo(x => x.Create <IFoo>(Argument.Is.Any <FakeObject>())) .Returns(this.fakeConfiguration); }
public void IsApplicableTo_should_return_result_from_call_matcher(bool callMatcherResult) { var call = ExpressionHelper.CreateFakeCall <IFoo>(x => x.Bar()); Fake.Configure(this.callMatcher) .CallsTo(x => x.Matches(call)) .Returns(callMatcherResult); var rule = this.CreateRule <IFoo>(x => x.Bar()); var result = rule.IsApplicableTo(call); Assert.That(result, Is.EqualTo(callMatcherResult)); }