/
AutoFakeItTests.cs
174 lines (142 loc) · 5.56 KB
/
AutoFakeItTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using FakeItEasy.AutoFakeIt.UnitTests.Stubs;
using FakeItEasy.Core;
using FluentAssertions;
using NUnit.Framework;
using System;
namespace FakeItEasy.AutoFakeIt.UnitTests.Specs
{
public class AutoFakeItTests
{
[Test]
public void GenerateShouldReturnAnInstanceOfClassWithoutDependencies()
{
var sut = new AutoFakeIt().Generate<SimpleSut>();
sut.Should().NotBeNull();
}
[Test]
public void GenerateShouldThrowExceptionWhenClassHasNoConstructorsWithFakeableParameters()
{
Action act = () => new AutoFakeIt().Generate<UnfakeableSut>();
act.Should().Throw<ArgumentException>()
.WithMessage($"No suitable constructor found for type '{typeof(UnfakeableSut).FullName}'.")
.WithInnerException<FakeCreationException>();
}
[Test]
public void GenerateShouldReturnAnInstanceOfClassWithFakedDependencies()
{
var sut = new AutoFakeIt().Generate<DependenciesSut>();
sut.Should().NotBeNull();
A.CallTo(() => sut.DependencyA.Method()).Returns("Faked");
sut.DependencyA.Method().Should().Be("Faked");
}
[Test]
public void GenerateShouldReturnAnInstanceOfClassWithSubdependencies()
{
var autoFakeIt = new AutoFakeIt();
var sut = autoFakeIt.Generate<SubdependenciesSut>();
sut.Should().NotBeNull();
A.CallTo(() => sut.Dependency.Subdependency.Method()).Returns("Faked");
sut.Dependency.Subdependency.Method().Should().Be("Faked");
}
[Test]
public void GenerateShouldReturnAnInstanceOfClassWithSubdependenciesDespiteItsOrder()
{
var autoFakeIt = new AutoFakeIt();
autoFakeIt.Resolve<Dependency>();
var sut = autoFakeIt.Generate<SubdependenciesSut>();
sut.Should().NotBeNull();
}
[Test]
public void GenerateShouldReturnAnInstanceOfClassWithTheMostFakedDependenciesPossible()
{
var sut = new AutoFakeIt().Generate<MultipleConstructorsSut>();
sut.Should().NotBeNull();
A.CallTo(() => sut.DependencyB.Method()).Returns("Faked");
sut.DependencyB.Method().Should().Be("Faked");
}
[Test]
public void GenerateShouldReusePreviouslyResolvedType()
{
var autoFakeIt = new AutoFakeIt();
var depA = autoFakeIt.Resolve<DependencyA>();
var sut = autoFakeIt.Generate<DependenciesSut>();
sut.DependencyA.Should().Be(depA);
}
[Test]
public void ResolveShouldReturnGeneratedFakeTypes()
{
var autoFakeIt = new AutoFakeIt();
var sut = autoFakeIt.Generate<DependenciesSut>();
autoFakeIt.Resolve<DependencyA>().Should().Be(sut.DependencyA);
}
[Test]
public void ResolveShouldGenerateAndReturnAFakeTypeWhenNoneIsAvailable()
{
var autoFakeIt = new AutoFakeIt();
var depA = autoFakeIt.Resolve<DependencyA>();
depA.Should().NotBeNull();
A.CallTo(() => depA.Method()).Returns("Faked");
depA.Method().Should().Be("Faked");
}
[Test]
public void ResolveShouldReturnProvidedDependency()
{
var autoFakeIt = new AutoFakeIt();
var depA = new DependencyA();
autoFakeIt.Provide(depA);
autoFakeIt.Resolve<DependencyA>().Should().Be(depA);
}
[Test]
public void ResolveShouldReturnProvidedValueTypeDependency()
{
var autoFakeIt = new AutoFakeIt();
var structDep = new ValueDependency();
autoFakeIt.Provide(structDep);
autoFakeIt.Resolve<ValueDependency>().Should().Be(structDep);
}
[Test]
public void GenerateShouldReturnProvidedValueTypeDependency()
{
var autoFakeIt = new AutoFakeIt();
var structDep = new ValueDependency();
autoFakeIt.Provide(structDep);
autoFakeIt.Generate<StructDependenciesSut>().ValueDependency.Should().Be(structDep);
}
[Test]
public void ResolveShouldUpdateProvidedDependencyWhenOneIsAlreadyAvailable()
{
var autoFakeIt = new AutoFakeIt();
var depA = new DependencyA();
autoFakeIt.Provide(depA);
var depA2 = new DependencyA();
autoFakeIt.Provide(depA2);
autoFakeIt.Resolve<DependencyA>().Should().Be(depA2);
}
[Test]
public void ResolveShouldReturnObjectsRegisteredWithExplicitTypes()
{
var autoFakeIt = new AutoFakeIt();
var depA = new DependencyA();
autoFakeIt.Provide<IDependencyA>(depA);
autoFakeIt.Resolve<IDependencyA>().Should().Be(depA);
}
[Test]
public void GenerateShouldUseProvidedDependency()
{
var autoFakeIt = new AutoFakeIt();
var depA = new DependencyA();
autoFakeIt.Provide(depA);
var sut = autoFakeIt.Generate<DependenciesSut>();
sut.DependencyA.Should().Be(depA);
}
[Test]
public void GenericResolveShouldWorkWithNonGenericProvide()
{
var autoFakeIt = new AutoFakeIt();
var depA = new DependencyA();
autoFakeIt.Provide(typeof(DependencyA), depA);
var resolvedDepA = autoFakeIt.Resolve<DependencyA>();
resolvedDepA.Should().NotBeNull();
}
}
}