Ik heb een vraag over het onderstaande probleem:
Ik zoek een manier om 'Foo' te testen zonder dat de 'Bar' method geraakt wordt.
Ik ben gekomen tot de onderstaande oplossing. Het nadeel is echter dat ik een interface nodig heb en mijzelf moet meegeven in elke method. Deze oplossing bevalt mij niet.
Heeft iemand suggesties hoe ik dit probleem het beste kan omzeilen?
code:
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
| public class OriginalSample { public bool Foo(ISomeEntity entity) { return entity.IsThatSo ? entity.IsThatSo : Bar(entity); } public bool Bar(ISomeEntity entity) { return entity.IsThatAsWell; } } [TestClass] public class OriginalSampleTest { [TestMethod] public void Foo_EntityWithSo_ReturnsTrue { // Arrange Mock<ISomeEntity> someEntityMock = new Mock<ISomeEntity>(); mock.SetupGet(m => m.IsThatSo).Returns( false ); mock.SetupGet(m => m.IsThatAsWell).Returns( true ); // Act private bool result = _OriginalSample.Foo( someEntityMock.Object ); // Assert Assert.IsTrue(result); } } |
Ik zoek een manier om 'Foo' te testen zonder dat de 'Bar' method geraakt wordt.
Ik ben gekomen tot de onderstaande oplossing. Het nadeel is echter dat ik een interface nodig heb en mijzelf moet meegeven in elke method. Deze oplossing bevalt mij niet.
Heeft iemand suggesties hoe ik dit probleem het beste kan omzeilen?
code:
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
| public interface INewSample { bool Foo(ISomeEntity entity, INewSample sample); bool Bar(ISomeEntity entity); } public class NewSample : INewSample { public bool Foo(ISomeEntity entity, INewSample sample) { return entity.IsThatSo ? entity.IsThatSo : sample.Bar(entity); } public bool Bar(ISomeEntity entity) { return entity.IsThatAsWell; } } [TestClass] public class NewSampleTest { [TestMethod] public void Foo_EntityWithSo_ReturnsTrue { // Arrange Mock<ISomeEntity> someEntityMock = new Mock<ISomeEntity>(); mock.SetupGet(m => m.IsThatSo).Returns( false ); Mock<INewSample> sampleMock = new Mock<INewSample>(); sampleMock.Setup(m => m.Bar).Returns(false).Verify(); // Act private bool result = _OriginalSample.Foo( someEntityMock.Object, sampleMock.Object ); // Assert (that the logic tried to use the 'bar' method sampleMock.Verify(); } } |
Feeling lonely and content at the same time, I believe, is a rare kind of happiness