/
IntegrationTestSqlServerDal.cs
71 lines (58 loc) · 2.05 KB
/
IntegrationTestSqlServerDal.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
using DAL.JecaestevezApp;
using DAL.JecaestevezApp.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace IntegrationTest.JecaestevezApp
{
[TestClass]
public class IntegrationTestSqlServerDal
{
private DbContextOptions<EfDbContext> _options;
[TestInitialize]
public void Init()
{
string randomDBName = "EFDatabaseFirstDB" + Guid.NewGuid();
_options = new DbContextOptionsBuilder<EfDbContext>()
.UseSqlServer($"Server=.;Database={randomDBName};Trusted_Connection=True;MultipleActiveResultSets=true")
.Options;
using (var context = new EfDbContext(_options))
{
context.Database.Migrate();
};
}
[TestMethod]
public void Given_NoItems_Them_AddNewItem()
{
var itemSaved = new Item();
//Arrange
var expirationDay = DateTime.Now.AddYears(1);
//Act
using (var context = new EfDbContext(_options))
{
var newItem = new Item()
{
Name = "Ron Palido",
Description = "Drink",
Expiration = expirationDay
};
context.Add(newItem);
context.SaveChanges();
itemSaved = context.Items.Find(1);
}
//Assert
Assert.IsNotNull(itemSaved, "Failed -Item not saved");
Assert.AreEqual(itemSaved.Name, "Ron Palido", "Failed - Errons in Field Name");
Assert.AreEqual(itemSaved.Description, "Drink", "Failed - Errons in Field Description");
Assert.AreEqual(itemSaved.Expiration, expirationDay, "Failed - Errons in Field expiration");
}
[TestCleanup]
public void Clean()
{
using (var context = new EfDbContext(_options))
{
context.Database.EnsureDeleted();
}
}
}
}