Skip to content

Commit 01f526d

Browse files
committed
Added Web layer additional tests
1 parent d3a2db1 commit 01f526d

13 files changed

Lines changed: 209 additions & 24 deletions

File tree

UnitTestingDemo/UnitTestingDemo.Data.Tests/Adapters/BookEntityDomainAdapterTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ public void TestInitialize()
1515
_bookEntityDomainAdapter = new BookEntityDomainAdapter();
1616
}
1717

18+
[TestMethod]
19+
public void Adapt_NullItem_ReturnsNull()
20+
{
21+
//Arrange
22+
BookEntity bookEntity = null;
23+
24+
//Act
25+
var book = _bookEntityDomainAdapter.Adapt(bookEntity);
26+
27+
//Assert
28+
Assert.IsNull(book);
29+
}
30+
1831
[TestMethod]
1932
public void Adapt_Id_IsEqual()
2033
{

UnitTestingDemo/UnitTestingDemo.Data/Adapters/BookDomainEntityAdapter.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ public class BookDomainEntityAdapter : IBookDomainEntityAdapter
99
{
1010
public BookEntity Adapt(Book book)
1111
{
12-
return new BookEntity
13-
{
14-
Id = book.Id,
15-
Title = book.Title,
16-
Author = book.Title
17-
};
12+
return book == null
13+
? null
14+
: new BookEntity
15+
{
16+
Id = book.Id,
17+
Title = book.Title,
18+
Author = book.Title
19+
};
1820
}
1921
}
2022
}

UnitTestingDemo/UnitTestingDemo.Data/Adapters/BookEntityDomainAdapter.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,14 @@ public class BookEntityDomainAdapter : IBookEntityDomainAdapter
99
{
1010
public Book Adapt(BookEntity bookEntity)
1111
{
12-
return new Book
13-
{
14-
Id = bookEntity.Id,
15-
Title = bookEntity.Title,
16-
Author = bookEntity.Author
17-
};
18-
}
19-
20-
public BookEntity FromDomain(Book book)
21-
{
22-
return new BookEntity
23-
{
24-
Id = book.Id,
25-
Title = book.Title,
26-
Author = book.Title
27-
};
12+
return bookEntity == null
13+
? null
14+
: new Book
15+
{
16+
Id = bookEntity.Id,
17+
Title = bookEntity.Title,
18+
Author = bookEntity.Author
19+
};
2820
}
2921
}
3022
}

UnitTestingDemo/UnitTestingDemo.Data/BookRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public IList<BookEntity> GetAll()
2525

2626
public BookEntity GetById(int id)
2727
{
28-
throw new NotImplementedException();
28+
return _bookContext.Books.FirstOrDefault(x => x.Id == id);
2929
}
3030

3131
public void Save(BookEntity saveThis)

UnitTestingDemo/UnitTestingDemo.Data/Contexts/BookContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ public BookContext(DbContextOptions<BookContext> options)
1111

1212
public DbSet<BookEntity> Books { get; set; }
1313
}
14-
1514
}

UnitTestingDemo/UnitTestingDemo.Services.Tests/BookServiceTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,42 @@ public void GetAll_ItemsInRepository_CallsAdapterSameNumberOfTimes()
6262
_mockBookEntityDomainAdapter.Verify(x=>x.Adapt(It.IsAny<BookEntity>()),
6363
Times.Exactly(sampleData.Count));
6464
}
65+
66+
[TestMethod]
67+
public void GetById_ItemsInRepository_ReturnsItem()
68+
{
69+
//arrange
70+
var bookEntity = new BookEntity();
71+
_mockBookRepository.Setup(x => x.GetById(It.IsAny<int>()))
72+
.Returns(bookEntity);
73+
_mockBookEntityDomainAdapter.Setup(x => x.Adapt(It.IsAny<BookEntity>()))
74+
.Returns(new Book());
75+
int id = 1;
76+
77+
//act
78+
var result = _bookService.GetById(id);
79+
80+
//assert
81+
Assert.IsNotNull(result);
82+
}
83+
84+
[TestMethod]
85+
public void GetById_NoItem_ReturnsNull()
86+
{
87+
//arrange
88+
var bookEntity = new BookEntity();
89+
_mockBookRepository.Setup(x => x.GetById(It.IsAny<int>()))
90+
.Returns(bookEntity);
91+
_mockBookEntityDomainAdapter.Setup(x => x.Adapt(It.IsAny<BookEntity>()))
92+
.Returns(new Book());
93+
int id = 1;
94+
95+
//act
96+
var result = _bookService.GetById(id);
97+
98+
//assert
99+
Assert.IsNotNull(result);
100+
}
101+
65102
}
66103
}

UnitTestingDemo/UnitTestingDemo.Services/BookService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ public IEnumerable<Book> GetAll()
2222
{
2323
return _bookRepository.GetAll().Select(b=>_bookEntityDomainAdapter.Adapt(b));
2424
}
25+
26+
public Book GetById(int id)
27+
{
28+
return _bookEntityDomainAdapter.Adapt(_bookRepository.GetById(id));
29+
}
2530
}
2631
}

UnitTestingDemo/UnitTestingDemo.Services/Interfaces/IBookService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ namespace UnitTestingDemo.Services.Interfaces
88
public interface IBookService
99
{
1010
IEnumerable<Book> GetAll();
11+
Book GetById(int id);
1112
}
1213
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
using UnitTestingDemo.Models;
8+
using UnitTestingDemo.Services.Interfaces;
9+
10+
namespace UnitTestingDemo.Controllers
11+
{
12+
public class BookController : Controller
13+
{
14+
private readonly IBookService _bookService;
15+
16+
public BookController(IBookService bookService)
17+
{
18+
_bookService = bookService;
19+
}
20+
// GET: Book
21+
public ActionResult Index()
22+
{
23+
var bookListViewModel = new BookListViewModel {Books = _bookService.GetAll()};
24+
return View(bookListViewModel);
25+
}
26+
27+
// GET: Book/Details/5
28+
public ActionResult Details(int id)
29+
{
30+
return View();
31+
}
32+
33+
// GET: Book/Create
34+
public ActionResult Create()
35+
{
36+
return View();
37+
}
38+
39+
// POST: Book/Create
40+
[HttpPost]
41+
[ValidateAntiForgeryToken]
42+
public ActionResult Create(IFormCollection collection)
43+
{
44+
try
45+
{
46+
// TODO: Add insert logic here
47+
48+
return RedirectToAction(nameof(Index));
49+
}
50+
catch
51+
{
52+
return View();
53+
}
54+
}
55+
56+
// GET: Book/Edit/5
57+
public ActionResult Edit(int id)
58+
{
59+
return View();
60+
}
61+
62+
// POST: Book/Edit/5
63+
[HttpPost]
64+
[ValidateAntiForgeryToken]
65+
public ActionResult Edit(int id, IFormCollection collection)
66+
{
67+
try
68+
{
69+
// TODO: Add update logic here
70+
71+
return RedirectToAction(nameof(Index));
72+
}
73+
catch
74+
{
75+
return View();
76+
}
77+
}
78+
79+
// GET: Book/Delete/5
80+
public ActionResult Delete(int id)
81+
{
82+
return View();
83+
}
84+
85+
// POST: Book/Delete/5
86+
[HttpPost]
87+
[ValidateAntiForgeryToken]
88+
public ActionResult Delete(int id, IFormCollection collection)
89+
{
90+
try
91+
{
92+
// TODO: Add delete logic here
93+
94+
return RedirectToAction(nameof(Index));
95+
}
96+
catch
97+
{
98+
return View();
99+
}
100+
}
101+
}
102+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
using UnitTestingDemo.Domain;
3+
4+
namespace UnitTestingDemo.Models
5+
{
6+
public class BookListViewModel
7+
{
8+
public IEnumerable<Book> Books { get; set; }
9+
}
10+
}

0 commit comments

Comments
 (0)