-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
239 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,123 @@ | ||
namespace TestProject; | ||
using NUnit.Framework; | ||
using Microsoft.EntityFrameworkCore; | ||
using dotnetapiapp.Controllers; | ||
using dotnetapiapp.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
public class Tests | ||
namespace dotnetapiapp.Tests | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
[TestFixture] | ||
public class BookControllerTests | ||
{ | ||
} | ||
private BookController _bookController; | ||
private BookStoreContext _context; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
// Initialize an in-memory database for testing | ||
var options = new DbContextOptionsBuilder<BookStoreContext>() | ||
.UseInMemoryDatabase(Guid.NewGuid().ToString()) | ||
.Options; | ||
|
||
_context = new BookStoreContext(options); | ||
_context.Database.EnsureCreated(); // Create the database | ||
|
||
// Seed the database with sample data | ||
_context.Books.AddRange(new List<Book> | ||
{ | ||
new Book { Id = 1, Title = "Book 1", Author = "Author 1", Price = 10.99m, Quantity = 5 }, | ||
new Book { Id = 2, Title = "Book 2", Author = "Author 2", Price = 12.99m, Quantity = 7 }, | ||
new Book { Id = 3, Title = "Book 3", Author = "Author 3", Price = 9.99m, Quantity = 3 }, | ||
}); | ||
_context.SaveChanges(); | ||
|
||
_bookController = new BookController(_context); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
_context.Database.EnsureDeleted(); // Delete the in-memory database after each test | ||
_context.Dispose(); | ||
} | ||
|
||
[Test] | ||
public async Task GetAllBooks_ReturnsOkResult() | ||
{ | ||
// Act | ||
var result = await _bookController.GetAllBooks(); | ||
|
||
// Assert | ||
Assert.IsInstanceOf<OkObjectResult>(result.Result); | ||
} | ||
|
||
[Test] | ||
public async Task GetAllBooks_ReturnsAllBooks() | ||
{ | ||
// Act | ||
var result = await _bookController.GetAllBooks(); | ||
|
||
// Assert | ||
Assert.IsInstanceOf<OkObjectResult>(result.Result); | ||
var okResult = result.Result as OkObjectResult; | ||
|
||
Assert.IsInstanceOf<IEnumerable<Book>>(okResult.Value); | ||
var books = okResult.Value as IEnumerable<Book>; | ||
|
||
var bookCount = books.Count(); | ||
Assert.AreEqual(3, bookCount); // Assuming you have 3 books in the seeded data | ||
} | ||
|
||
[Test] | ||
public async Task GetBookById_ExistingId_ReturnsOkResult() | ||
{ | ||
// Arrange | ||
var existingId = 1; | ||
|
||
// Act | ||
var result = await _bookController.GetBookById(existingId); | ||
|
||
// Assert | ||
Assert.IsInstanceOf<OkObjectResult>(result.Result); | ||
} | ||
|
||
[Test] | ||
public async Task GetBookById_ExistingId_ReturnsBook() | ||
{ | ||
// Arrange | ||
var existingId = 1; | ||
|
||
// Act | ||
var result = await _bookController.GetBookById(existingId); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
|
||
Assert.IsInstanceOf<OkObjectResult>(result.Result); | ||
var okResult = result.Result as OkObjectResult; | ||
|
||
var book = okResult.Value as Book; | ||
Assert.IsNotNull(book); | ||
Assert.AreEqual(existingId, book.Id); | ||
} | ||
|
||
[Test] | ||
public async Task GetBookById_NonExistingId_ReturnsNotFound() | ||
{ | ||
// Arrange | ||
var nonExistingId = 99; // Assuming this ID does not exist in the seeded data | ||
|
||
// Act | ||
var result = await _bookController.GetBookById(nonExistingId); | ||
|
||
// Assert | ||
Assert.IsInstanceOf<NotFoundResult>(result.Result); | ||
} | ||
|
||
[Test] | ||
public void Test1() | ||
{ | ||
Assert.Pass(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using dotnetmvcapp.Controllers; | ||
using dotnetmvcapp.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
|
||
namespace dotnetmvcapp.Tests | ||
{ | ||
[TestFixture] | ||
public class BookControllerTests | ||
{ | ||
private BookController _bookController; | ||
private HttpClient _httpClient; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
// Create an instance of the BookController | ||
_bookController = new BookController(); | ||
|
||
// Create an HttpClient instance with custom handler for bypassing certificate validation | ||
var httpClientHandler = new HttpClientHandler(); | ||
httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true; | ||
_httpClient = new HttpClient(httpClientHandler); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
// Dispose of the HttpClient | ||
_httpClient.Dispose(); | ||
} | ||
|
||
[Test] | ||
public void Index_ReturnsViewWithListOfBooks() | ||
{ | ||
// Act | ||
var result = _bookController.Index(); | ||
|
||
// Assert | ||
Assert.IsInstanceOf<ViewResult>(result); | ||
|
||
var viewResult = (ViewResult)result; | ||
Assert.IsInstanceOf<List<Book>>(viewResult.Model); | ||
} | ||
|
||
[Test] | ||
public void Search_ValidId_ReturnsViewWithBook() | ||
{ | ||
// Arrange | ||
int validId = 1; // Replace with a valid book ID | ||
|
||
// Act | ||
var result = _bookController.Search(validId); | ||
|
||
// Assert | ||
Assert.IsInstanceOf<ViewResult>(result); | ||
|
||
var viewResult = (ViewResult)result; | ||
Assert.IsInstanceOf<List<Book>>(viewResult.Model); | ||
Assert.IsTrue(((List<Book>)viewResult.Model).Count > 0); | ||
} | ||
|
||
[Test] | ||
public void Search_InvalidId_ReturnsViewWithEmptyList() | ||
{ | ||
// Arrange | ||
int invalidId = -1; // Replace with an invalid book ID | ||
|
||
// Act | ||
var result = _bookController.Search(invalidId); | ||
|
||
// Assert | ||
Assert.IsInstanceOf<ViewResult>(result); | ||
|
||
var viewResult = (ViewResult)result; | ||
Assert.IsInstanceOf<List<Book>>(viewResult.Model); | ||
Assert.AreEqual(0, ((List<Book>)viewResult.Model).Count); | ||
} | ||
[Test] | ||
public void Search_NonexistentId_ReturnsNotFound() | ||
{ | ||
// Arrange | ||
int nonexistentId = 9999; // Replace with a non-existent book ID | ||
|
||
// Act | ||
var result = _bookController.Search(nonexistentId); | ||
Assert.IsInstanceOf<ViewResult>(result); | ||
|
||
var viewResult = (ViewResult)result; | ||
Assert.IsInstanceOf<List<Book>>(viewResult.Model); | ||
Assert.AreEqual(0, ((List<Book>)viewResult.Model).Count); | ||
} | ||
|
||
[Test] | ||
public void Search_ValidId_ReturnsCorrectBook() | ||
{ | ||
// Arrange | ||
int validId = 3; // Replace with a valid book ID | ||
|
||
// Act | ||
var result = _bookController.Search(validId); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
Assert.IsInstanceOf<ViewResult>(result); | ||
|
||
var viewResult = (ViewResult)result; | ||
Assert.IsInstanceOf<List<Book>>(viewResult.Model); | ||
Assert.AreEqual(1, ((List<Book>)viewResult.Model).Count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
2.0 | ||
2.0 |