public void Interceptions_should_return_interceptions_that_are_added()
        {
            var fake = new FakeObject(typeof(IFoo));

            var one = CreateApplicableInterception();
            var two = CreateApplicableInterception();

            fake.AddRule(one);
            fake.AddRule(two);

            Assert.That(fake.Rules, Is.EquivalentTo(new[] { one, two }));
        }
        public void The_first_applicable_interceptor_should_be_called_when_it_has_not_been_used()
        {
            var fake = new FakeObject(typeof(IFoo));

            var interception = new FakeCallRule
            {
                IsApplicableTo = x => true
            };

            fake.AddRule(NonApplicableInterception);
            fake.AddRule(interception);

            ((IFoo)fake.Object).Bar();

            Assert.That(interception.ApplyWasCalled, Is.True);
        }
        public void The_latest_added_rule_should_be_called_for_ever_when_no_number_of_times_is_specified()
        {
            var fake = new FakeObject(typeof(IFoo));

            var firstRule  = CreateApplicableInterception();
            var latestRule = CreateApplicableInterception();

            fake.AddRule(firstRule);
            fake.AddRule(latestRule);

            var foo = (IFoo)fake.Object;

            foo.Bar();
            foo.Bar();
            foo.Bar();

            Assert.That(firstRule.ApplyWasCalled, Is.False);
        }
Exemplo n.º 4
0
        public static T Fake <T>(T wrappedInstance, IFakeCallRecorder persister) where T : class
        {
            var fakeObject = new FakeObject(typeof(T));

            fakeObject.AddRule(new PersisterRule {
                Recorder = persister, WrappedObjectRule = new WrappedObjectRule(wrappedInstance)
            });
            return((T)fakeObject.Object);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a fake version of the object where calls can be configured.
        /// Any unconfigured calls will call the wrapped instance directly.
        /// </summary>
        /// <typeparam name="T">The type of fake to generate.</typeparam>
        /// <param name="wrappedInstance">The object to wrap.</param>
        /// <returns>A fake object.</returns>
        public static T Fake <T>(T wrappedInstance) where T : class
        {
            Guard.IsNotNull(wrappedInstance, "wrappedInstance");

            var fakeObject = new FakeObject(typeof(T));

            fakeObject.AddRule(new WrappedObjectRule(wrappedInstance));
            return((T)fakeObject.Object);
        }
        public void An_applicable_action_should_be_called_its_specified_number_of_times_before_the_next_applicable_action_is_called()
        {
            var fake = new FakeObject(typeof(IFoo));

            var applicableTwice = new FakeCallRule
            {
                IsApplicableTo      = x => true,
                NumberOfTimesToCall = 2
            };

            var nextApplicable = CreateApplicableInterception();

            fake.AddRule(nextApplicable);
            fake.AddRule(applicableTwice);

            ((IFoo)fake.Object).Bar();
            ((IFoo)fake.Object).Bar();
            Assert.That(nextApplicable.ApplyWasCalled, Is.False);

            ((IFoo)fake.Object).Bar();
            Assert.That(nextApplicable.ApplyWasCalled, Is.True);
        }
        public void Rules_should_only_be_valid_within_the_current_scope()
        {
            var fake = new FakeObject(typeof(IFoo));
            var rule = A.Fake <IFakeObjectCallRule>();

            Fake.Configure(rule)
            .CallsTo(x => x.IsApplicableTo(Argument.Is.Any <IFakeObjectCall>()))
            .Returns(true);

            using (Fake.CreateScope())
            {
                fake.AddRule(rule);
            }

            (fake.Object as IFoo).Bar();

            Fake.Assert(rule).WasNotCalled(x => x.Apply(Argument.Is.Any <IWritableFakeObjectCall>()));
        }
        public void DefaultValue_should_be_returned_when_the_last_applicable_action_has_been_used_its_specified_number_of_times()
        {
            var fake = new FakeObject(typeof(IFoo));

            var applicableTwice = CreateApplicableInterception();

            applicableTwice.NumberOfTimesToCall = 2;
            applicableTwice.Apply = x => x.SetReturnValue(10);

            fake.AddRule(applicableTwice);

            ((IFoo)fake.Object).Baz();
            ((IFoo)fake.Object).Baz();

            var result = ((IFoo)fake.Object).Baz();

            Assert.That(result, Is.EqualTo(0));
        }