Escali License control 1
RegionServiceTest.cs
Gå til dokumentasjonen til denne filen.
1using System.Threading.Tasks;
6using Xunit;
7
9{
10
15{
21 private async Task<DataContext> InsertData(DataContext context)
22 {
23 context.Regions.Add(new Region() { Country = "NOR", Area = "OSLO" });
24 context.Regions.Add(new Region() { Country = "NOR", Area = "BERGEN" });
25 await context.SaveChangesAsync();
26 return context;
27 }
28
32 [Fact]
34 {
35 // Arrange
37 await InsertData(context);
38
39 // Act
40 var regionService = new RegionService(context);
41 var res = regionService.GetAllRegions();
42
43 // Assert
44 Assert.NotEmpty(res.Result);
45
46 }
47
51 [Fact]
53 {
55 await InsertData(context);
56 var reg = context.Regions.FindAsync(1).Result.Area;
57
58 var regionService = new RegionService(context);
59 var res = await regionService.GetAllRegions();
60
61 Assert.NotEmpty(res);
62 Assert.Equal(reg, res[0].Area);
63 Assert.Equal("BERGEN", res[1].Area);
64 }
65
69 [Fact]
71 {
73 await InsertData(context);
74
75 var findIdOne = context.Regions.FindAsync(1).Result;
76
77 var regionService = new RegionService(context);
78 var res = await regionService.GetAllRegions();
79
80 // Returns true on Bergen == Bergen
81 Assert.Equal(res[0].Area, findIdOne.Area);
82
83 findIdOne.Area = "Tønsberg";
84 regionService.UpdateRegion(findIdOne);
85
86 var resUpdate = await regionService.GetAllRegions();
87
88 // Region Id 1 updated to Tønsberg, returns true on Tønsberg == Tønsberg
89 Assert.Equal(resUpdate[0].Area, findIdOne.Area);
90 // Confirms that the updated city with id=1 is no longer Bergen, return true on Bergen != Tønsberg
91 Assert.NotEqual("BERGEN", resUpdate[0].Area);
92
93 }
94
95 }
96}
97
var context
Definition: Program.cs:49
RegionService class inserts and updates Region in the Database
async void UpdateRegion_FindRegionByIdAndUpdateCity()
Passes if regionlist is notempty and region with id is found and the updated region has new expected ...
async void GetAllRegions_ContainingSameCountry()
Passes if regionlist is not empty after inserting
async void FindRegionById_CityEqualsOsloAndSecondEntryIsBergen()
Passes if retrieving regionlist is not empty and arenames of entries in regionlist is what is expecte...
async Task< DataContext > InsertData(DataContext context)
Inserts two regions into the context
static DataContext GetContext()
Creates a testcontext for testing
Definition: TestBase.cs:14