@@ -18,198 +18,211 @@ Example Usage
18
18
-------------
19
19
20
20
Given the following code:
21
-
22
- public interface IDependency1
23
- {
24
- int SomeMethod(int i);
25
- }
26
-
27
- public interface IDependency2
21
+ ``` c#
22
+ public interface IDependency1
23
+ {
24
+ int SomeMethod (int i );
25
+ }
26
+
27
+ public interface IDependency2
28
+ {
29
+ int SomeOtherMethod ();
30
+ }
31
+
32
+ public class MyClass
33
+ {
34
+ private readonly IDependency1 _d1 ;
35
+ private readonly IDependency2 _d2 ;
36
+
37
+ public MyClass (IDependency1 d1 , IDependency2 d2 )
28
38
{
29
- int SomeOtherMethod();
39
+ _d1 = d1 ;
40
+ _d2 = d2 ;
30
41
}
31
42
32
- public class MyClass
43
+ public int AMethod ()
33
44
{
34
- private readonly IDependency1 _d1;
35
- private readonly IDependency2 _d2;
36
-
37
- public MyClass(IDependency1 d1, IDependency2 d2)
38
- {
39
- _d1 = d1;
40
- _d2 = d2;
41
- }
42
-
43
- public int AMethod()
44
- {
45
- return _d1.SomeMethod(_d2.SomeOtherMethod());
46
- }
45
+ return _d1 .SomeMethod (_d2 .SomeOtherMethod ());
47
46
}
47
+ }
48
+ ```
48
49
49
50
Then consider the following test which outlines how to use the ` AutoSubstitute ` class:
50
51
51
- [Test]
52
- public void Example_test_with_standard_resolve()
53
- {
54
- const int val = 3;
55
- var autoSubstitute = new AutoSubstitute();
56
- autoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val);
57
- autoSubstitute.Resolve<IDependency1>().SomeMethod(val).Returns(c => c.Arg<int>());
52
+ ``` c#
53
+ [Test ]
54
+ public void Example_test_with_standard_resolve ()
55
+ {
56
+ const int val = 3 ;
57
+ var autoSubstitute = new AutoSubstitute ();
58
+ autoSubstitute .Resolve <IDependency2 >().SomeOtherMethod ().Returns (val );
59
+ autoSubstitute .Resolve <IDependency1 >().SomeMethod (val ).Returns (c => c .Arg <int >());
58
60
59
- var result = autoSubstitute.Resolve<MyClass>().AMethod();
61
+ var result = autoSubstitute .Resolve <MyClass >().AMethod ();
60
62
61
- Assert.That(result, Is.EqualTo(val));
62
- }
63
+ Assert .That (result , Is .EqualTo (val ));
64
+ }
65
+ ```
63
66
64
67
You can also provide concrete classes to ` AutoSubstitute ` either explicitly or by type, consider the following code to add the code above:
65
68
66
- public class Dependency2 : IDependency2
69
+ ``` c#
70
+ public class Dependency2 : IDependency2
71
+ {
72
+ public const int Value = 10 ;
73
+
74
+ public int SomeOtherMethod ()
67
75
{
68
- public const int Value = 10;
76
+ return Value ;
77
+ }
78
+ }
79
+
80
+ public class ConcreteClass
81
+ {
82
+ private readonly int _i ;
69
83
70
- public int SomeOtherMethod()
71
- {
72
- return Value;
73
- }
84
+ public ConcreteClass (int i )
85
+ {
86
+ _i = i ;
74
87
}
75
88
76
- public class ConcreteClass
89
+ public virtual int Add ( int val )
77
90
{
78
- private readonly int _i;
91
+ return val + _i ;
92
+ }
93
+ }
79
94
80
- public ConcreteClass(int i)
81
- {
82
- _i = i ;
83
- }
95
+ public class MyClassWithConcreteDependency
96
+ {
97
+ private readonly ConcreteClass _c ;
98
+ private readonly IDependency2 _d2 ;
84
99
85
- public virtual int Add(int val )
86
- {
87
- return val + _i ;
88
- }
100
+ public MyClassWithConcreteDependency ( ConcreteClass c , IDependency2 d2 )
101
+ {
102
+ _c = c ;
103
+ _d2 = d2 ;
89
104
}
90
105
91
- public class MyClassWithConcreteDependency
106
+ public int AMethod ()
92
107
{
93
- private readonly ConcreteClass _c;
94
- private readonly IDependency2 _d2;
95
-
96
- public MyClassWithConcreteDependency(ConcreteClass c, IDependency2 d2)
97
- {
98
- _c = c;
99
- _d2 = d2;
100
- }
101
-
102
- public int AMethod()
103
- {
104
- return _c.Add(_d2.SomeOtherMethod());
105
- }
108
+ return _c .Add (_d2 .SomeOtherMethod ());
106
109
}
110
+ }
111
+ ```
107
112
108
113
And then the following tests:
109
114
110
- [Test]
111
- public void Example_test_with_concrete_type_provided()
112
- {
113
- const int val = 3;
114
- var autoSubstitute = new AutoSubstitute();
115
- autoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val); // This shouldn't do anything because of the next line
116
- autoSubstitute.Provide<IDependency2, Dependency2>();
117
- autoSubstitute.Resolve<IDependency1>().SomeMethod(Arg.Any<int>()).Returns(c => c.Arg<int>());
115
+ ``` c#
116
+ [Test ]
117
+ public void Example_test_with_concrete_type_provided ()
118
+ {
119
+ const int val = 3 ;
120
+ var autoSubstitute = new AutoSubstitute ();
121
+ autoSubstitute .Resolve <IDependency2 >().SomeOtherMethod ().Returns (val ); // This shouldn't do anything because of the next line
122
+ autoSubstitute .Provide <IDependency2 , Dependency2 >();
123
+ autoSubstitute .Resolve <IDependency1 >().SomeMethod (Arg .Any <int >()).Returns (c => c .Arg <int >());
118
124
119
- var result = autoSubstitute.Resolve<MyClass>().AMethod();
125
+ var result = autoSubstitute .Resolve <MyClass >().AMethod ();
120
126
121
- Assert.That(result, Is.EqualTo(Dependency2.Value));
122
- }
127
+ Assert .That (result , Is .EqualTo (Dependency2 .Value ));
128
+ }
123
129
124
- [Test]
125
- public void Example_test_with_concrete_object_provide()
126
- {
127
- const int val1 = 3;
128
- const int val2 = 2;
129
- var autoSubstitute = new AutoSubstitute();
130
- autoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);
131
- autoSubstitute.Provide(new ConcreteClass(val2));
130
+ [Test ]
131
+ public void Example_test_with_concrete_object_provide ()
132
+ {
133
+ const int val1 = 3 ;
134
+ const int val2 = 2 ;
135
+ var autoSubstitute = new AutoSubstitute ();
136
+ autoSubstitute .Resolve <IDependency2 >().SomeOtherMethod ().Returns (val1 );
137
+ autoSubstitute .Provide (new ConcreteClass (val2 ));
132
138
133
- var result = autoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
139
+ var result = autoSubstitute .Resolve <MyClassWithConcreteDependency >().AMethod ();
134
140
135
- Assert.That(result, Is.EqualTo(val1 + val2));
136
- }
141
+ Assert .That (result , Is .EqualTo (val1 + val2 ));
142
+ }
143
+ ```
137
144
138
145
There is also a convenient syntax for registering and resolving a ` Substitute.For<T>() ` with the underlying container for concrete classes:
139
146
140
- [Test]
141
- public void Example_test_with_substitute_for_concrete()
142
- {
143
- const int val1 = 3;
144
- const int val2 = 2;
145
- const int val3 = 10;
146
- var autoSubstitute = new AutoSubstitute();
147
- autoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val1);
148
- autoSubstitute.SubstituteFor<ConcreteClass>(val2).Add(Arg.Any<int>()).Returns(val3);
147
+ ``` c#
148
+ [Test ]
149
+ public void Example_test_with_substitute_for_concrete ()
150
+ {
151
+ const int val1 = 3 ;
152
+ const int val2 = 2 ;
153
+ const int val3 = 10 ;
154
+ var autoSubstitute = new AutoSubstitute ();
155
+ autoSubstitute .Resolve <IDependency2 >().SomeOtherMethod ().Returns (val1 );
156
+ autoSubstitute .SubstituteFor <ConcreteClass >(val2 ).Add (Arg .Any <int >()).Returns (val3 );
149
157
150
- var result = autoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
158
+ var result = autoSubstitute .Resolve <MyClassWithConcreteDependency >().AMethod ();
151
159
152
- Assert.That(result, Is.EqualTo(val3));
153
- }
160
+ Assert .That (result , Is .EqualTo (val3 ));
161
+ }
162
+ ```
154
163
155
164
Similarly, you can resolve a concrete type from the autosubstitute container and register that with the underlying container using the ` ResolveAndSubstituteFor ` method:
156
165
157
- public class ConcreteClassWithDependency
166
+ ``` c#
167
+ public class ConcreteClassWithDependency
168
+ {
169
+ private readonly IDependency1 _dependency ;
170
+ private readonly int _i ;
171
+
172
+ public ConcreteClassWithDependency (IDependency1 dependency , int i )
158
173
{
159
- private readonly IDependency1 _dependency;
160
- private readonly int _i;
161
-
162
- public ConcreteClassWithDependency(IDependency1 dependency, int i)
163
- {
164
- _dependency = dependency;
165
- _i = i;
166
- }
167
-
168
- public int Double()
169
- {
170
- return _dependency.SomeMethod(_i)*2;
171
- }
174
+ _dependency = dependency ;
175
+ _i = i ;
172
176
}
173
177
174
- public class MyClassWithConcreteDependencyThatHasDependencies
178
+ public int Double ()
175
179
{
176
- private readonly ConcreteClassWithDependency _c;
177
- private readonly IDependency2 _d2;
178
-
179
- public MyClassWithConcreteDependencyThatHasDependencies(ConcreteClassWithDependency c, IDependency2 d2)
180
- {
181
- _c = c;
182
- _d2 = d2;
183
- }
184
-
185
- public int AMethod()
186
- {
187
- return _d2.SomeOtherMethod() * _c.Double();
188
- }
180
+ return _dependency .SomeMethod (_i )* 2 ;
189
181
}
182
+ }
190
183
191
- ...
184
+ public class MyClassWithConcreteDependencyThatHasDependencies
185
+ {
186
+ private readonly ConcreteClassWithDependency _c ;
187
+ private readonly IDependency2 _d2 ;
192
188
193
- [Test]
194
- public void Example_test_with_substitute_for_concrete_resolved_from_autofac()
189
+ public MyClassWithConcreteDependencyThatHasDependencies (ConcreteClassWithDependency c , IDependency2 d2 )
195
190
{
196
- const int val1 = 2;
197
- const int val2 = 3;
198
- const int val3 = 4;
199
- var AutoSubstitute = new AutoSubstitute();
200
- // Much better / more maintainable than:
201
- //AutoSubstitute.SubstituteFor<ConcreteClassWithDependency>(AutoSubstitute.Resolve<IDependency1>(), val1);
202
- AutoSubstitute.ResolveAndSubstituteFor<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1));
203
- AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
204
- AutoSubstitute.Resolve<IDependency1>().SomeMethod(val1).Returns(val3);
205
-
206
- var result = AutoSubstitute.Resolve<MyClassWithConcreteDependencyThatHasDependencies>().AMethod();
207
-
208
- Assert.That(result, Is.EqualTo(val2*val3*2));
191
+ _c = c ;
192
+ _d2 = d2 ;
209
193
}
210
194
195
+ public int AMethod ()
196
+ {
197
+ return _d2 .SomeOtherMethod () * _c .Double ();
198
+ }
199
+ }
200
+
201
+ .. .
202
+
203
+ [Test ]
204
+ public void Example_test_with_substitute_for_concrete_resolved_from_autofac ()
205
+ {
206
+ const int val1 = 2 ;
207
+ const int val2 = 3 ;
208
+ const int val3 = 4 ;
209
+ var AutoSubstitute = new AutoSubstitute ();
210
+ // Much better / more maintainable than:
211
+ // AutoSubstitute.SubstituteFor<ConcreteClassWithDependency>(AutoSubstitute.Resolve<IDependency1>(), val1);
212
+ AutoSubstitute .ResolveAndSubstituteFor <ConcreteClassWithDependency >(new TypedParameter (typeof (int ), val1 ));
213
+ AutoSubstitute .Resolve <IDependency2 >().SomeOtherMethod ().Returns (val2 );
214
+ AutoSubstitute .Resolve <IDependency1 >().SomeMethod (val1 ).Returns (val3 );
215
+
216
+ var result = AutoSubstitute .Resolve <MyClassWithConcreteDependencyThatHasDependencies >().AMethod ();
217
+
218
+ Assert .That (result , Is .EqualTo (val2 * val3 * 2 ));
219
+ }
220
+ ```
221
+
211
222
If you need to access the underlying Autofac container for some reason then you use the ` Container ` property on the ` AutoSubstitute ` object.
212
223
213
224
If you want to make modifications to the container builder before the container is build from it there is a second constructor parameter you can use, for example:
214
225
215
- var autoSubstitute = new AutoSubstitute(cb => cb.RegisterModule<SomeModule>());
226
+ ``` c#
227
+ var autoSubstitute = new AutoSubstitute (cb => cb .RegisterModule <SomeModule >());
228
+ ```
0 commit comments