Skip to content

Commit 8a83838

Browse files
committed
Use builder
1 parent a781bde commit 8a83838

10 files changed

+344
-317
lines changed

AutofacContrib.NSubstitute.Tests/AutoSubstituteCollectionFixture.cs

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -79,96 +79,76 @@ public TestIReadOnlyListComponent(IReadOnlyList<IServiceItem> serviceItems)
7979
[Test]
8080
public void TestIEnumerableCorrectlyResolves()
8181
{
82-
using (var autosub = new AutoSubstitute(b =>
83-
{
84-
b.Provide<IServiceItem, ServiceItemA>();
85-
b.Provide<IServiceItem, ServiceItemB>();
86-
}))
87-
{
88-
var mockA = autosub.Resolve<ServiceItemA>();
89-
var mockB = autosub.Resolve<ServiceItemB>();
90-
var component = autosub.Resolve<TestIEnumerableComponent>();
82+
using var autosub = AutoSubstitute.Configure()
83+
.Provide<IServiceItem, ServiceItemA>(out var mockA)
84+
.Provide<IServiceItem, ServiceItemB>(out var mockB)
85+
.Build();
9186

92-
Assert.That(component.ServiceItems, Is.Not.Empty);
93-
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
94-
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
95-
}
87+
var component = autosub.Resolve<TestIEnumerableComponent>();
88+
89+
Assert.That(component.ServiceItems, Is.Not.Empty);
90+
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
91+
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
9692
}
9793

9894
[Test]
9995
public void TestIListCorrectlyResolves()
10096
{
101-
using (var autosub = new AutoSubstitute(b =>
102-
{
103-
b.Provide<IServiceItem, ServiceItemA>();
104-
b.Provide<IServiceItem, ServiceItemB>();
105-
}))
106-
{
107-
var mockA = autosub.Resolve<ServiceItemA>();
108-
var mockB = autosub.Resolve<ServiceItemB>();
109-
var component = autosub.Resolve<TestIListComponent>();
97+
using var autosub = AutoSubstitute.Configure()
98+
.Provide<IServiceItem, ServiceItemA>(out var mockA)
99+
.Provide<IServiceItem, ServiceItemB>(out var mockB)
100+
.Build();
110101

111-
Assert.That(component.ServiceItems, Is.Not.Empty);
112-
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
113-
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
114-
}
102+
var component = autosub.Resolve<TestIListComponent>();
103+
104+
Assert.That(component.ServiceItems, Is.Not.Empty);
105+
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
106+
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
115107
}
116108

117109
[Test]
118110
public void TestIReadOnlyCollectionCorrectlyResolves()
119111
{
120-
using (var autosub = new AutoSubstitute(b =>
121-
{
122-
b.Provide<IServiceItem, ServiceItemA>();
123-
b.Provide<IServiceItem, ServiceItemB>();
124-
}))
125-
{
126-
var mockA = autosub.Resolve<ServiceItemA>();
127-
var mockB = autosub.Resolve<ServiceItemB>();
128-
var component = autosub.Resolve<TestIReadOnlyCollectionComponent>();
112+
using var autosub = AutoSubstitute.Configure()
113+
.Provide<IServiceItem, ServiceItemA>(out var mockA)
114+
.Provide<IServiceItem, ServiceItemB>(out var mockB)
115+
.Build();
129116

130-
Assert.That(component.ServiceItems, Is.Not.Empty);
131-
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
132-
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
133-
}
117+
var component = autosub.Resolve<TestIReadOnlyCollectionComponent>();
118+
119+
Assert.That(component.ServiceItems, Is.Not.Empty);
120+
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
121+
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
134122
}
135123

136124
[Test]
137125
public void TestICollectionCorrectlyResolves()
138126
{
139-
using (var autosub = new AutoSubstitute(b =>
140-
{
141-
b.Provide<IServiceItem, ServiceItemA>();
142-
b.Provide<IServiceItem, ServiceItemB>();
143-
}))
144-
{
145-
var mockA = autosub.Resolve<ServiceItemA>();
146-
var mockB = autosub.Resolve<ServiceItemB>();
147-
var component = autosub.Resolve<TestICollectionComponent>();
127+
using var autosub = AutoSubstitute.Configure()
128+
.Provide<IServiceItem, ServiceItemA>(out var mockA)
129+
.Provide<IServiceItem, ServiceItemB>(out var mockB)
130+
.Build();
148131

149-
Assert.That(component.ServiceItems, Is.Not.Empty);
150-
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
151-
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
152-
}
132+
var component = autosub.Resolve<TestICollectionComponent>();
133+
134+
Assert.That(component.ServiceItems, Is.Not.Empty);
135+
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
136+
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
153137
}
154138

155139
[Test]
156140
public void TestIReadOnlyListCorrectlyResolves()
157141
{
158-
using (var autosub = new AutoSubstitute(b =>
159-
{
160-
b.Provide<IServiceItem, ServiceItemA>();
161-
b.Provide<IServiceItem, ServiceItemB>();
162-
}))
163-
{
164-
var mockA = autosub.Resolve<ServiceItemA>();
165-
var mockB = autosub.Resolve<ServiceItemB>();
166-
var component = autosub.Resolve<TestIReadOnlyListComponent>();
142+
using var autosub = AutoSubstitute.Configure()
143+
.Provide<IServiceItem, ServiceItemA>(out var mockA)
144+
.Provide<IServiceItem, ServiceItemB>(out var mockB)
145+
.Build();
167146

168-
Assert.That(component.ServiceItems, Is.Not.Empty);
169-
Assert.That(component.ServiceItems.Contains(mockA), Is.True);
170-
Assert.That(component.ServiceItems.Contains(mockB), Is.True);
171-
}
147+
var component = autosub.Resolve<TestIReadOnlyListComponent>();
148+
149+
Assert.That(component.ServiceItems, Is.Not.Empty);
150+
Assert.That(component.ServiceItems.Contains(mockA.Value), Is.True);
151+
Assert.That(component.ServiceItems.Contains(mockB.Value), Is.True);
172152
}
173153
}
174154
}

AutofacContrib.NSubstitute.Tests/AutoSubstituteFixture.cs

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,75 +49,66 @@ public void RunAll()
4949
[Test]
5050
public void DefaultConstructorIsLoose()
5151
{
52-
using (var mock = new AutoSubstitute())
53-
{
54-
RunWithSingleSetupationTest(mock);
55-
}
52+
using var mock = new AutoSubstitute();
53+
54+
RunWithSingleSetupationTest(mock);
5655
}
5756

5857
[Test]
5958
public void ProvideMock()
6059
{
6160
var mockA = Substitute.For<IServiceA>();
6261

63-
using (var autoSubstitute = new AutoSubstitute(b =>
64-
{
65-
b.Provide(mockA);
66-
}))
67-
{
68-
var component = autoSubstitute.Resolve<TestComponent>();
69-
component.RunAll();
62+
using var autoSubstitute = AutoSubstitute.Configure()
63+
.Provide(mockA)
64+
.Build();
7065

71-
mockA.Received().RunA();
72-
}
66+
var component = autoSubstitute.Resolve<TestComponent>();
67+
component.RunAll();
68+
69+
mockA.Received().RunA();
7370
}
7471

7572
[Test]
7673
public void ProvideImplementation()
7774
{
78-
using (var mock = new AutoSubstitute(b =>
79-
{
80-
b.Provide<IServiceA, ServiceA>();
81-
}))
82-
{
83-
var serviceA = mock.Resolve<ServiceA>();
75+
using var mock = AutoSubstitute.Configure()
76+
.Provide<IServiceA, ServiceA>(out var serviceA)
77+
.Build();
8478

85-
Assert.IsNotNull(serviceA);
86-
Assert.IsFalse(serviceA is ICallRouter);
87-
}
79+
Assert.IsNotNull(serviceA.Value);
80+
Assert.IsFalse(serviceA.Value is ICallRouter);
8881
}
8982

9083
[Test]
9184
public void DefaultConstructorWorksWithAllTests()
9285
{
93-
using (var mock = new AutoSubstitute())
94-
{
95-
RunTest(mock);
96-
}
86+
using var mock = new AutoSubstitute();
87+
88+
RunTest(mock);
9789
}
9890

9991
[Test]
10092
public void WorksWithUnmetSetupations()
10193
{
102-
using (var loose = new AutoSubstitute())
103-
{
104-
RunWithSingleSetupationTest(loose);
105-
}
94+
using var loose = new AutoSubstitute();
95+
96+
RunWithSingleSetupationTest(loose);
10697
}
10798

10899
[Test]
109100
public void NormalSetupationsAreVerified()
110101
{
111-
using (var mock = new AutoSubstitute())
112-
{
113-
Assert.That(() => SetUpSetupations(mock), Throws.TypeOf<ReceivedCallsException>());
114-
}
102+
using var mock = new AutoSubstitute();
103+
104+
Assert.That(() => SetUpSetupations(mock), Throws.TypeOf<ReceivedCallsException>());
115105
}
116106

117107
[Test]
118108
public void ProperInitializationIsPerformed()
119109
{
120-
var autoSubstitute = new AutoSubstitute();
110+
using var autoSubstitute = new AutoSubstitute();
111+
121112
Assert.IsNotNull(autoSubstitute.Container);
122113
}
123114

AutofacContrib.NSubstitute.Tests/ExampleFixture.cs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,15 @@ public void Example_test_with_standard_resolve()
127127
public void Example_test_with_concrete_type_provided()
128128
{
129129
const int val = 3;
130-
var AutoSubstitute = new AutoSubstitute(b =>
131-
{
132-
b.Provide<IDependency2, Dependency2>();
133-
});
134-
AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val); // This shouldn't do anything because of the next line
135-
AutoSubstitute.Resolve<IDependency1>().SomeMethod(Arg.Any<int>()).Returns(c => c.Arg<int>());
136130

137-
var result = AutoSubstitute.Resolve<MyClass>().AMethod();
131+
using var mock = AutoSubstitute.Configure()
132+
.Provide<IDependency2, Dependency2>(out _)
133+
.Build();
134+
135+
mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val); // This shouldn't do anything because of the next line
136+
mock.Resolve<IDependency1>().SomeMethod(Arg.Any<int>()).Returns(c => c.Arg<int>());
137+
138+
var result = mock.Resolve<MyClass>().AMethod();
138139

139140
Assert.That(result, Is.EqualTo(Dependency2.Value));
140141
}
@@ -145,14 +146,13 @@ public void Example_test_with_concrete_object_provided()
145146
const int val1 = 3;
146147
const int val2 = 2;
147148

148-
var AutoSubstitute = new AutoSubstitute(b =>
149-
{
150-
b.Provide(new ConcreteClass(val2));
151-
});
149+
var mock = AutoSubstitute.Configure()
150+
.Provide(new ConcreteClass(val2))
151+
.Build();
152152

153-
AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);
153+
mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);
154154

155-
var result = AutoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
155+
var result = mock.Resolve<MyClassWithConcreteDependency>().AMethod();
156156

157157
Assert.That(result, Is.EqualTo(val1 + val2));
158158
}
@@ -163,14 +163,14 @@ public void Example_test_with_substitute_for_concrete()
163163
const int val1 = 3;
164164
const int val2 = 2;
165165
const int val3 = 10;
166-
var AutoSubstitute = new AutoSubstitute(b =>
167-
{
168-
b.SubstituteFor<ConcreteClass>(val2).Add(Arg.Any<int>()).Returns(val3);
169-
});
170166

171-
AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);
167+
using var utoSubstitute = AutoSubstitute.Configure()
168+
.SubstituteFor<ConcreteClass>(val2).Configure(c => c.Add(Arg.Any<int>()).Returns(val3))
169+
.Build();
170+
171+
utoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);
172172

173-
var result = AutoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
173+
var result = utoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
174174

175175
Assert.That(result, Is.EqualTo(val3));
176176
}
@@ -181,16 +181,15 @@ public void Example_test_with_substitute_for_concrete_resolved_from_autofac()
181181
const int val1 = 2;
182182
const int val2 = 3;
183183
const int val3 = 4;
184-
var AutoSubstitute = new AutoSubstitute(b =>
185-
{
186-
b.ResolveAndSubstituteFor<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1));
187-
});
188-
// Much better / more maintainable than:
189-
//AutoSubstitute.SubstituteFor<ConcreteClassWithDependency>(AutoSubstitute.Resolve<IDependency1>(), val1);
190-
AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
191-
AutoSubstitute.Resolve<IDependency1>().SomeMethod(val1).Returns(val3);
192-
193-
var result = AutoSubstitute.Resolve<MyClassWithConcreteDependencyThatHasDependencies>().AMethod();
184+
185+
using var mock = AutoSubstitute.Configure()
186+
.ResolveAndSubstituteFor<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1))
187+
.Build();
188+
189+
mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
190+
mock.Resolve<IDependency1>().SomeMethod(val1).Returns(val3);
191+
192+
var result = mock.Resolve<MyClassWithConcreteDependencyThatHasDependencies>().AMethod();
194193

195194
Assert.That(result, Is.EqualTo(val2 * val3 * 2));
196195
}

AutofacContrib.NSubstitute.Tests/KeyedRegistrationFixture.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ public static void ShouldResolveASubstituteForIndexedDependency()
5151
public static void ShouldAcceptProvidedIndexedDependency()
5252
{
5353
var substitute = Substitute.For<IDependency2>();
54-
var autoSubstitute = new AutoSubstitute(b =>
55-
{
56-
b.Provide(substitute, Switch.On);
57-
});
54+
55+
using var autoSubstitute = AutoSubstitute.Configure()
56+
.Provide(substitute, Switch.On)
57+
.Build();
58+
5859
substitute.SomeOtherMethod().Returns(5);
5960

6061
var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>();

0 commit comments

Comments
 (0)