Skip to content

Commit 705a0c6

Browse files
committed
Added additional tests
1 parent 7b1a518 commit 705a0c6

7 files changed

Lines changed: 236 additions & 3 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using Moq;
7+
using UnitTestingDemo.Data.Contexts;
8+
using UnitTestingDemo.Data.EntityModels;
9+
10+
namespace UnitTestingDemo.Data.Tests
11+
{
12+
[TestClass]
13+
public class BookRepositoryTestsWithInMemoryDB
14+
{
15+
readonly List<BookEntity> _books = new List<BookEntity>
16+
{
17+
new BookEntity
18+
{
19+
Id = 1,
20+
Title = "Hamlet",
21+
Author = new AuthorEntity
22+
{
23+
Id = 1,
24+
FirstName = "William",
25+
LastName = "Shakespeare"
26+
}
27+
},
28+
new BookEntity
29+
{
30+
Id = 2,
31+
Title = "A Midsummer Night's Dream",
32+
Author = new AuthorEntity
33+
{
34+
Id = 1,
35+
FirstName = "William",
36+
LastName = "Shakespeare"
37+
}
38+
}
39+
};
40+
41+
[TestMethod]
42+
public void GetAll_WithItems_returnsAll()
43+
{
44+
//arrange
45+
var options = new DbContextOptionsBuilder<BookContext>()
46+
.UseInMemoryDatabase("GetAllTest")
47+
.Options;
48+
using (var context = new BookContext(options))
49+
{
50+
context.Books.AddRange(_books);
51+
context.SaveChanges();
52+
}
53+
54+
//act
55+
IList<BookEntity> result;
56+
using (var context = new BookContext(options))
57+
{
58+
var bookRepository = new BookRepository(context);
59+
result = bookRepository.GetAll();
60+
}
61+
62+
//assert
63+
Assert.AreEqual(_books.Count(), result.Count);
64+
}
65+
}
66+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Moq;
6+
using UnitTestingDemo.Data.Contexts;
7+
using UnitTestingDemo.Data.EntityModels;
8+
9+
namespace UnitTestingDemo.Data.Tests
10+
{
11+
[TestClass]
12+
public class BookRepositoryTestsWithMocks
13+
{
14+
private BookRepository _bookRepository;
15+
private Mock<BookContext> _mockBookContext;
16+
private IQueryable<BookEntity> _books;
17+
18+
[TestInitialize]
19+
public void TestInitialize()
20+
{
21+
_books = new List<BookEntity>
22+
{
23+
new BookEntity
24+
{
25+
Id = 1,
26+
Title = "Hamlet",
27+
Author = new AuthorEntity
28+
{
29+
Id = 1,
30+
FirstName = "William",
31+
LastName = "Shakespeare"
32+
}
33+
},
34+
new BookEntity
35+
{
36+
Id = 2,
37+
Title = "A Midsummer Night's Dream",
38+
Author = new AuthorEntity
39+
{
40+
Id = 1,
41+
FirstName = "William",
42+
LastName = "Shakespeare"
43+
}
44+
}
45+
}.AsQueryable();
46+
47+
var mockSet = new Mock<DbSet<BookEntity>>();
48+
mockSet.As<IQueryable<BookEntity>>().Setup(m => m.Provider)
49+
.Returns(_books.Provider);
50+
mockSet.As<IQueryable<BookEntity>>().Setup(m => m.Expression)
51+
.Returns(_books.Expression);
52+
mockSet.As<IQueryable<BookEntity>>().Setup(m => m.ElementType)
53+
.Returns(_books.ElementType);
54+
mockSet.As<IQueryable<BookEntity>>().Setup(m => m.GetEnumerator())
55+
.Returns(_books.GetEnumerator());
56+
57+
_mockBookContext = new Mock<BookContext>();
58+
_mockBookContext.Setup(c => c.Books)
59+
.Returns(mockSet.Object);
60+
61+
_bookRepository = new BookRepository(_mockBookContext.Object);
62+
}
63+
64+
[TestMethod]
65+
public void GetAll_WithItems_returnsAll()
66+
{
67+
//arrange
68+
69+
//act
70+
var result = _bookRepository.GetAll();
71+
72+
//assert
73+
Assert.AreEqual(_books.Count(), result.Count);
74+
}
75+
}
76+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using EntityFrameworkCoreMock;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using Moq;
7+
using UnitTestingDemo.Data.Contexts;
8+
using UnitTestingDemo.Data.EntityModels;
9+
10+
namespace UnitTestingDemo.Data.Tests
11+
{
12+
[TestClass]
13+
public class BookRepositoryTestsWithMocks2
14+
{
15+
private BookRepository _bookRepository;
16+
private Mock<BookContext> _mockBookContext;
17+
private IQueryable<BookEntity> _books;
18+
19+
[TestInitialize]
20+
public void TestInitialize()
21+
{
22+
_books = new List<BookEntity>
23+
{
24+
new BookEntity
25+
{
26+
Id = 1,
27+
Title = "Hamlet",
28+
Author = new AuthorEntity
29+
{
30+
Id = 1,
31+
FirstName = "William",
32+
LastName = "Shakespeare"
33+
}
34+
},
35+
new BookEntity
36+
{
37+
Id = 2,
38+
Title = "A Midsummer Night's Dream",
39+
Author = new AuthorEntity
40+
{
41+
Id = 1,
42+
FirstName = "William",
43+
LastName = "Shakespeare"
44+
}
45+
}
46+
}.AsQueryable();
47+
48+
var _mockBookContext = new DbContextMock<BookContext>(new DbContextOptionsBuilder<BookContext>().Options);
49+
var bookDbSetMock = _mockBookContext.CreateDbSetMock(x => x.Books, _books);
50+
_bookRepository = new BookRepository(_mockBookContext.Object);
51+
}
52+
53+
[TestMethod]
54+
public void GetAll_WithItems_returnsAll()
55+
{
56+
//arrange
57+
58+
//act
59+
var result = _bookRepository.GetAll();
60+
61+
//assert
62+
Assert.AreEqual(_books.Count(), result.Count);
63+
}
64+
}
65+
}

UnitTestingDemo/UnitTestingDemo.Data.Tests/UnitTestingDemo.Data.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="EntityFrameworkCoreMock.Moq" Version="1.0.0.20" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.0" />
1012
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
1113
<PackageReference Include="Moq" Version="4.10.1" />
1214
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />

UnitTestingDemo/UnitTestingDemo.Data/Contexts/BookContext.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ namespace UnitTestingDemo.Data.Contexts
55
{
66
public class BookContext : DbContext
77
{
8+
public BookContext()
9+
{ }
10+
811
public BookContext(DbContextOptions<BookContext> options)
912
: base(options)
1013
{ }
1114

12-
public DbSet<BookEntity> Books { get; set; }
13-
public DbSet<AuthorEntity> Authors { get; set; }
15+
public virtual DbSet<BookEntity> Books { get; set; }
16+
public virtual DbSet<AuthorEntity> Authors { get; set; }
1417
}
1518
}

UnitTestingDemo/UnitTestingDemo.Data/EntityModels/BookEntity.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System.ComponentModel.DataAnnotations;
2+
using System.Diagnostics.CodeAnalysis;
23
using UnitTestingDemo.Data.Interfaces;
34

45
namespace UnitTestingDemo.Data.EntityModels
56
{
67
[ExcludeFromCodeCoverage]
78
public class BookEntity : IIntId
89
{
10+
[Key]
911
public int Id { get; set; }
12+
[Required]
1013
public string Title { get; set; }
1114
public int AuthorId { get; set; }
1215
public virtual AuthorEntity Author { get; set; }

UnitTestingDemo/UnitTestingDemo.Tests/Controllers/BookControllerTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,23 @@ public void Index_Returns_Model()
5151
Assert.IsNotNull(response.Model);
5252
Assert.IsInstanceOfType(response.Model, typeof(BookListViewModel));
5353
}
54+
55+
[TestMethod]
56+
public void Index_calls_BookService()
57+
{
58+
//arrange
59+
var bookList = new List<Book>
60+
{
61+
new Book{Id = 1, Title = "Testing", AuthorId = 1}
62+
};
63+
_mockBookService.Setup(x => x.GetAll())
64+
.Returns(bookList);
65+
66+
//act
67+
var response = _bookController.Index() as ViewResult;
68+
69+
//assert
70+
_mockBookService.Verify(x=>x.GetAll(), Times.Once);
71+
}
5472
}
5573
}

0 commit comments

Comments
 (0)