|
1 | | -//using Microsoft.VisualStudio.TestTools.UnitTesting; |
2 | | -//using EntityFrameworkWrapper; |
3 | | -//using System; |
4 | | -//using System.Collections.Generic; |
5 | | -//using System.Linq; |
6 | | -//using System.Text; |
7 | | -//using System.Threading.Tasks; |
8 | | -//using Moq; |
9 | | -//using System.Data.Entity; |
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using EntityFrameworkWrapper; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Moq; |
| 9 | +using System.Data.Entity; |
10 | 10 |
|
11 | | -//namespace EntityFrameworkWrapper.Tests |
12 | | -//{ |
13 | | -//public class InputModel{public int Id {get;set;} public string Name{get;set;}} |
14 | | -//[TestClass] |
15 | | -// public class RepositoryTest |
16 | | -// { |
17 | | -// private Mock<IDbContext> _IMockCtx; |
| 11 | +namespace EntityFrameworkWrapper.Tests |
| 12 | +{ |
| 13 | + public class InputModel { public int Id { get; set; } public string Name { get; set; } } |
| 14 | + [TestClass] |
| 15 | + public class RepositoryTest |
| 16 | + { |
| 17 | + private Mock<IDbContext> _IMockCtx; |
18 | 18 |
|
19 | | -// private IEnumerable<InputModel> model; |
20 | | -// [TestInitialize] |
21 | | -// public void Init() |
22 | | -// { |
23 | | -// _IMockCtx = new Mock<IDbContext>(); |
24 | | -// model =new List<InputModel>{ new InputModel { |
25 | | -// Name = "Test Name", |
26 | | -// Id = 999} |
27 | | -// }; |
28 | | -// SetRepository(model); |
29 | | -// } |
30 | | -// private void SetRepository<T>(IEnumerable<T> data) where T : class |
31 | | -// { |
32 | | -// var dbset = GetIDbset(data); |
33 | | -// _IMockCtx.Setup(m => m.Set<T>()).Returns(dbset); |
34 | | -// _IMockCtx.Setup(m => m.Set<T>().Add(It.IsAny<T>())).Verifiable(); |
35 | | -// _IMockCtx.Setup(m => m.Set<T>().Remove(It.IsAny<T>())).Verifiable(); |
36 | | -// _IMockCtx.Setup(m => m.SaveAsync()).ReturnsAsync(1); |
37 | | -// } |
| 19 | + private IEnumerable<InputModel> model; |
| 20 | + [TestInitialize] |
| 21 | + public void Init() |
| 22 | + { |
| 23 | + _IMockCtx = new Mock<IDbContext>(); |
| 24 | + model = new List<InputModel>{ new InputModel { |
| 25 | + Name = "Test Name", |
| 26 | + Id = 999} |
| 27 | + }; |
| 28 | + SetRepository(model); |
| 29 | + } |
| 30 | + private void SetRepository<T>(IEnumerable<T> data) where T : class |
| 31 | + { |
| 32 | + var dbset = GetIDbset(data); |
| 33 | + _IMockCtx.Setup(m => m.Set<T>()).Returns(dbset); |
| 34 | + _IMockCtx.Setup(m => m.Set<T>().Add(It.IsAny<T>())).Verifiable(); |
| 35 | + _IMockCtx.Setup(m => m.Set<T>().Remove(It.IsAny<T>())).Verifiable(); |
| 36 | + _IMockCtx.Setup(m => m.Save()).Returns(1); |
| 37 | + } |
38 | 38 |
|
39 | | -// private static IDbSet<T> GetIDbset<T>(IEnumerable<T> sourceList) where T : class |
40 | | -// { |
41 | | -// var queryable = sourceList.AsQueryable(); |
| 39 | + private static IDbSet<T> GetIDbset<T>(IEnumerable<T> sourceList) where T : class |
| 40 | + { |
| 41 | + var queryable = sourceList.AsQueryable(); |
42 | 42 |
|
43 | | -// var dbSet = new Mock<IDbSet<T>>(); |
44 | | -// dbSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(queryable.Provider); |
45 | | -// dbSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(queryable.Expression); |
46 | | -// dbSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(queryable.ElementType); |
47 | | -// dbSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(() => queryable.GetEnumerator()); |
48 | | -// dbSet.Setup(m => m.Add(It.IsAny<T>())).Callback<T>((s) => sourceList.ToList().Add(s)).Returns(() => { return sourceList.Last(); }); |
49 | | -// dbSet.Setup(m => m.Attach(It.IsAny<T>())).Callback<T>((s) => sourceList.ToList().Add(s)).Returns(() => { return sourceList.Last(); }); |
50 | | -// dbSet.Setup(m => m.Remove(It.IsAny<T>())).Callback<T>((s) => sourceList.ToList().Remove(s)).Returns(() => { return sourceList.Last(); }); |
51 | | -// return dbSet.Object; |
52 | | -// } |
53 | | -// [TestMethod] |
54 | | -// public void Get_client_data_from_datastore() |
55 | | -// { |
56 | | -// //Arrange |
57 | | -// var repository = new Repository<InputModel>(_IMockCtx.Object); |
| 43 | + var dbSet = new Mock<IDbSet<T>>(); |
| 44 | + dbSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(queryable.Provider); |
| 45 | + dbSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(queryable.Expression); |
| 46 | + dbSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(queryable.ElementType); |
| 47 | + dbSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(() => queryable.GetEnumerator()); |
| 48 | + dbSet.Setup(m => m.Add(It.IsAny<T>())).Callback<T>((s) => sourceList.ToList().Add(s)).Returns(() => { return sourceList.Last(); }); |
| 49 | + dbSet.Setup(m => m.Attach(It.IsAny<T>())).Callback<T>((s) => sourceList.ToList().Add(s)).Returns(() => { return sourceList.Last(); }); |
| 50 | + dbSet.Setup(m => m.Remove(It.IsAny<T>())).Callback<T>((s) => sourceList.ToList().Remove(s)).Returns(() => { return sourceList.Last(); }); |
| 51 | + return dbSet.Object; |
| 52 | + } |
| 53 | + [TestMethod] |
| 54 | + public void Get_client_data_from_datastore() |
| 55 | + { |
| 56 | + //Arrange |
| 57 | + var repository = new Repository<InputModel>(_IMockCtx.Object); |
58 | 58 |
|
59 | | -// //Act |
60 | | -// var result = repository.GetAsync(); |
| 59 | + //Act |
| 60 | + var result = repository.Get(); |
61 | 61 |
|
62 | | -// //Assert |
63 | | -// Assert.IsInstanceOfType(result, typeof(Task<IEnumerable<InputModel>>)); |
64 | | -// _IMockCtx.Verify(v => v.Set<InputModel>(), Times.Once); |
65 | | -// } |
| 62 | + //Assert |
| 63 | + Assert.IsInstanceOfType(result, typeof(IEnumerable<InputModel>)); |
| 64 | + _IMockCtx.Verify(v => v.Set<InputModel>(), Times.Once); |
| 65 | + } |
66 | 66 |
|
67 | | -// [TestMethod] |
68 | | -// public void Add_client_data_to_datastore_successful() |
69 | | -// { |
70 | | -// //Arrange |
71 | | -// var repository = new Repository<InputModel>(_IMockCtx.Object); |
72 | | -// var data = model.FirstOrDefault(); |
73 | | -// //Act |
74 | | -// var result=repository.Add(data); |
| 67 | + [TestMethod] |
| 68 | + public void Add_client_data_to_datastore_successful() |
| 69 | + { |
| 70 | + //Arrange |
| 71 | + var repository = new Repository<InputModel>(_IMockCtx.Object); |
| 72 | + var data = model.FirstOrDefault(); |
| 73 | + //Act |
| 74 | + repository.Add(data); |
75 | 75 |
|
76 | | -// //Assert |
77 | | -// Assert.IsInstanceOfType(result, typeof(Task<InputModel>)); |
78 | | -// _IMockCtx.Verify(v => v.Set<InputModel>().Add(It.IsAny<InputModel>()), Times.Once); |
79 | | -// } |
| 76 | + //Assert |
| 77 | + _IMockCtx.Verify(v => v.Set<InputModel>().Add(It.IsAny<InputModel>()), Times.Once); |
| 78 | + } |
80 | 79 |
|
81 | | -// [TestMethod] |
82 | | -// public void Delete_client_data_from_datastore_successful() |
83 | | -// { |
84 | | -// //Arrange |
85 | | -// var repository = new Repository<InputModel>(_IMockCtx.Object); |
| 80 | + [TestMethod] |
| 81 | + public void Delete_client_data_from_datastore_successful() |
| 82 | + { |
| 83 | + //Arrange |
| 84 | + var repository = new Repository<InputModel>(_IMockCtx.Object); |
86 | 85 |
|
87 | | -// //Act |
88 | | -// repository.Remove(1); |
| 86 | + //Act |
| 87 | + repository.Delete(new InputModel { }); |
89 | 88 |
|
90 | | -// //Assert |
91 | | -// _IMockCtx.Verify(v => v.Set<InputModel>().Remove(It.IsAny<InputModel>()), Times.Once); |
92 | | -// } |
93 | | -// }} |
| 89 | + //Assert |
| 90 | + _IMockCtx.Verify(v => v.Set<InputModel>().Remove(It.IsAny<InputModel>()), Times.Once); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments