|
| 1 | +using Autofac.Core.Registration; |
| 2 | +using NUnit.Framework; |
| 3 | +using System.Collections.Generic; |
| 4 | + |
| 5 | +namespace AutofacContrib.NSubstitute.Tests |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + public class TypesToSkipForMockingFixture |
| 9 | + { |
| 10 | + [Test] |
| 11 | + public void WithoutOption() |
| 12 | + { |
| 13 | + var mock = AutoSubstitute.Configure() |
| 14 | + .Provide<IDependency, Impl1>(out var impl1) |
| 15 | + .Provide<IDependency, Impl2>(out var impl2) |
| 16 | + .Build(); |
| 17 | + |
| 18 | + var items = mock.Resolve<IDependency[]>(); |
| 19 | + |
| 20 | + Assert.AreEqual(impl1.Value, items[0]); |
| 21 | + Assert.AreEqual(impl2.Value, items[1]); |
| 22 | + Assert.That(items[2], Is.NSubstituteMock); |
| 23 | + } |
| 24 | + |
| 25 | + [Test] |
| 26 | + public void ManuallyAddTypeToSkip() |
| 27 | + { |
| 28 | + var mock = AutoSubstitute.Configure() |
| 29 | + .ConfigureOptions(options => |
| 30 | + { |
| 31 | + options.TypesToSkipForMocking.Add(typeof(IDependency)); |
| 32 | + }) |
| 33 | + .Build(); |
| 34 | + |
| 35 | + Assert.Throws<ComponentNotRegisteredException>(() => mock.Resolve<IDependency>()); |
| 36 | + } |
| 37 | + |
| 38 | + [Test] |
| 39 | + public void DisableMockGenerationOnProvide() |
| 40 | + { |
| 41 | + var mock = AutoSubstitute.Configure() |
| 42 | + .ConfigureOptions(options => |
| 43 | + { |
| 44 | + options.AutomaticallySkipMocksForProvidedValues = true; |
| 45 | + }) |
| 46 | + .Provide<IDependency, Impl1>(out var impl1) |
| 47 | + .Provide<IDependency, Impl2>(out var impl2) |
| 48 | + .Build(); |
| 49 | + |
| 50 | + var items = mock.Resolve<IEnumerable<IDependency>>(); |
| 51 | + |
| 52 | + CollectionAssert.AreEqual(items, new[] { impl1.Value, impl2.Value }); |
| 53 | + } |
| 54 | + |
| 55 | + public interface IDependency |
| 56 | + { |
| 57 | + } |
| 58 | + |
| 59 | + public class Impl1 : IDependency |
| 60 | + { |
| 61 | + } |
| 62 | + |
| 63 | + public class Impl2 : IDependency |
| 64 | + { |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments