Skip to content

Commit 627bc51

Browse files
authored
Merge pull request #30 from odannyc/master
Adds some syntax highlighting to README
2 parents 040a7d3 + 747227c commit 627bc51

File tree

1 file changed

+154
-141
lines changed

1 file changed

+154
-141
lines changed

README.md

Lines changed: 154 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -18,198 +18,211 @@ Example Usage
1818
-------------
1919

2020
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)
2838
{
29-
int SomeOtherMethod();
39+
_d1 = d1;
40+
_d2 = d2;
3041
}
3142

32-
public class MyClass
43+
public int AMethod()
3344
{
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());
4746
}
47+
}
48+
```
4849

4950
Then consider the following test which outlines how to use the `AutoSubstitute` class:
5051

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>());
5860

59-
var result = autoSubstitute.Resolve<MyClass>().AMethod();
61+
var result = autoSubstitute.Resolve<MyClass>().AMethod();
6062

61-
Assert.That(result, Is.EqualTo(val));
62-
}
63+
Assert.That(result, Is.EqualTo(val));
64+
}
65+
```
6366

6467
You can also provide concrete classes to `AutoSubstitute` either explicitly or by type, consider the following code to add the code above:
6568

66-
public class Dependency2 : IDependency2
69+
```c#
70+
public class Dependency2 : IDependency2
71+
{
72+
public const int Value = 10;
73+
74+
public int SomeOtherMethod()
6775
{
68-
public const int Value = 10;
76+
return Value;
77+
}
78+
}
79+
80+
public class ConcreteClass
81+
{
82+
private readonly int _i;
6983

70-
public int SomeOtherMethod()
71-
{
72-
return Value;
73-
}
84+
public ConcreteClass(int i)
85+
{
86+
_i = i;
7487
}
7588

76-
public class ConcreteClass
89+
public virtual int Add(int val)
7790
{
78-
private readonly int _i;
91+
return val + _i;
92+
}
93+
}
7994

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;
8499

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;
89104
}
90105

91-
public class MyClassWithConcreteDependency
106+
public int AMethod()
92107
{
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());
106109
}
110+
}
111+
```
107112

108113
And then the following tests:
109114

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>());
118124

119-
var result = autoSubstitute.Resolve<MyClass>().AMethod();
125+
var result = autoSubstitute.Resolve<MyClass>().AMethod();
120126

121-
Assert.That(result, Is.EqualTo(Dependency2.Value));
122-
}
127+
Assert.That(result, Is.EqualTo(Dependency2.Value));
128+
}
123129

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));
132138

133-
var result = autoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
139+
var result = autoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
134140

135-
Assert.That(result, Is.EqualTo(val1 + val2));
136-
}
141+
Assert.That(result, Is.EqualTo(val1 + val2));
142+
}
143+
```
137144

138145
There is also a convenient syntax for registering and resolving a `Substitute.For<T>()` with the underlying container for concrete classes:
139146

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);
149157

150-
var result = autoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
158+
var result = autoSubstitute.Resolve<MyClassWithConcreteDependency>().AMethod();
151159

152-
Assert.That(result, Is.EqualTo(val3));
153-
}
160+
Assert.That(result, Is.EqualTo(val3));
161+
}
162+
```
154163

155164
Similarly, you can resolve a concrete type from the autosubstitute container and register that with the underlying container using the `ResolveAndSubstituteFor` method:
156165

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)
158173
{
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;
172176
}
173177

174-
public class MyClassWithConcreteDependencyThatHasDependencies
178+
public int Double()
175179
{
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;
189181
}
182+
}
190183

191-
...
184+
public class MyClassWithConcreteDependencyThatHasDependencies
185+
{
186+
private readonly ConcreteClassWithDependency _c;
187+
private readonly IDependency2 _d2;
192188

193-
[Test]
194-
public void Example_test_with_substitute_for_concrete_resolved_from_autofac()
189+
public MyClassWithConcreteDependencyThatHasDependencies(ConcreteClassWithDependency c, IDependency2 d2)
195190
{
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;
209193
}
210194

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+
211222
If you need to access the underlying Autofac container for some reason then you use the `Container` property on the `AutoSubstitute` object.
212223

213224
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:
214225

215-
var autoSubstitute = new AutoSubstitute(cb => cb.RegisterModule<SomeModule>());
226+
```c#
227+
var autoSubstitute = new AutoSubstitute(cb => cb.RegisterModule<SomeModule>());
228+
```

0 commit comments

Comments
 (0)