1
+ using System . Collections . Generic ;
2
+ using Model ;
3
+ using Moq ;
4
+ using Repository ;
5
+ using Services ;
6
+ using Xunit ;
7
+
8
+ namespace Tests . Unit . Service
9
+ {
10
+ public class PortfolioEntryServiceTest
11
+ {
12
+ [ Fact ]
13
+ public void Create_CallsRepository ( )
14
+ {
15
+ // arrange
16
+ var portfolioEntryToBeAdded = new PortfolioEntry ( "btc" , 1 ) ;
17
+ var repositoryMock = new Mock < IPortfolioEntryRepository > ( ) ;
18
+ repositoryMock . Setup ( x =>
19
+ x . Add ( It . Is < PortfolioEntry > ( portfolioEntry => portfolioEntry == portfolioEntryToBeAdded ) ) ) . Returns ( 1 ) ;
20
+ var service = new PortfolioEntryServiceImpl ( repositoryMock . Object ) ;
21
+
22
+ // act
23
+ var portfolioEntry = service . CreatePortfolioEntry ( "btc" , 1 ) ;
24
+
25
+ // assert
26
+ Assert . Equal ( portfolioEntryToBeAdded with { Id = 1 } , portfolioEntry ) ;
27
+ }
28
+
29
+ [ Fact ]
30
+ public void Get_CallsRepository ( )
31
+ {
32
+ // arrange
33
+ var portfolioEntryPresentInRepository = new PortfolioEntry ( "btc" , 1 ) ;
34
+ var repositoryMock = new Mock < IPortfolioEntryRepository > ( ) ;
35
+ repositoryMock . Setup ( x => x . Get ( It . Is < int > ( id => id == 1 ) ) ) . Returns ( portfolioEntryPresentInRepository ) ;
36
+ var service = new PortfolioEntryServiceImpl ( repositoryMock . Object ) ;
37
+
38
+ // act
39
+ var portfolioEntry = service . GetPortfolioEntry ( 1 ) ;
40
+
41
+ // assert
42
+ Assert . Equal ( portfolioEntryPresentInRepository , portfolioEntry ) ;
43
+ }
44
+
45
+ [ Fact ]
46
+ public void GetPortfolioEntries_CallsRepository ( )
47
+ {
48
+ // arrange
49
+ var entriesList = new List < PortfolioEntry >
50
+ {
51
+ new ( "btc" , 1 , 1 ) ,
52
+ new ( "ada" , 2 , 2 ) ,
53
+ new ( "btc" , 3 , 3 ) ,
54
+ new ( "ltc" , 1 , 4 )
55
+ } ;
56
+
57
+ var repositoryMock = new Mock < IPortfolioEntryRepository > ( ) ;
58
+ repositoryMock . Setup ( x => x . GetAllByPortfolioId ( It . Is < int > ( id => id == 1 ) ) ) . Returns (
59
+ new List < PortfolioEntry > ( )
60
+ {
61
+ entriesList [ 0 ] , entriesList [ 3 ]
62
+ } ) ;
63
+ var service = new PortfolioEntryServiceImpl ( repositoryMock . Object ) ;
64
+
65
+ // act
66
+ var entriesFetched = service . GetPortfolioEntries ( 1 ) ;
67
+
68
+ // assert
69
+ Assert . Equal ( new List < PortfolioEntry >
70
+ {
71
+ entriesList [ 0 ] , entriesList [ 3 ]
72
+ } , entriesFetched ) ;
73
+ }
74
+
75
+ [ Fact ]
76
+ public void Update_CallsRepository ( )
77
+ {
78
+ // arrange
79
+ var entryToBeUpdated = new PortfolioEntry ( "btc" , 1 , 1 ) ;
80
+ var repositoryMock = new Mock < IPortfolioEntryRepository > ( ) ;
81
+ repositoryMock . Setup ( x => x . Update ( It . IsAny < PortfolioEntry > ( ) ) ) . Returns ( true ) ;
82
+ var service = new PortfolioEntryServiceImpl ( repositoryMock . Object ) ;
83
+
84
+ // act
85
+ var updated = service . UpdatePortfolio ( entryToBeUpdated ) ;
86
+
87
+ // assert
88
+ Assert . True ( updated ) ;
89
+ }
90
+
91
+ [ Fact ]
92
+ public void Delete_CallsRepository ( )
93
+ {
94
+ // arrange
95
+ var entryToBeUpdated = new PortfolioEntry ( "btc" , 1 , 1 ) ;
96
+ var repositoryMock = new Mock < IPortfolioEntryRepository > ( ) ;
97
+ repositoryMock . Setup ( x => x . Delete ( It . IsAny < PortfolioEntry > ( ) ) ) . Returns ( true ) ;
98
+ var service = new PortfolioEntryServiceImpl ( repositoryMock . Object ) ;
99
+
100
+ // act
101
+ var delete = service . DeletePortfolioEntry ( entryToBeUpdated ) ;
102
+
103
+ // assert
104
+ Assert . True ( delete ) ;
105
+ }
106
+ }
107
+ }
0 commit comments