This project demonstrates how to test the integration of an ASP.Net WEB API with a SQL Server database. It covers various aspects of integration testing using Entity Framework and NUnit.
- SQL Server: Used for database management and integration.
- REST API: For creating and testing RESTful services.
- Entity Framework: ORM used for managing database interactions.
- NUnit Tests: Framework for writing unit and integration tests for .NET.
- ASP.Net: For building and testing web applications.
The project under test is called "Homies", an ASP.NET application. The key components include:
-
Homies.program.cs: This file initializes and runs the application.
- Areas: Groups different functional segments of the application for modularity.
- Controllers: Manages HTTP requests and responses, coordinating data flow between models and views.
- Data: Contains the data access layer, including the database context and migrations.
- Models: Defines the application's data structures and business logic.
- Views: Contains the UI elements, including HTML templates and Razor views.
-
Homies.Tests: This directory includes integration tests to verify that various components function correctly together.
- EventControllerTests.cs: Contains tests for the Controller to verify correct request handling.
- EventServiceTests.cs: Tests the Service, ensuring business logic and data processing work as expected.
- Visual Studio with ASP.NET
- Microsoft SQL Server Management Studio (SSMS)
- Docker (optional, for using a Docker container instead of a local SQL Server instance)
- Local SQL Server instance
You can also run the SQL Server using a Docker image, which simplifies the setup and ensures consistency across different environments. To set up SQL Server with Docker, follow these steps:
-
Pull the SQL Server Docker image:
docker pull mcr.microsoft.com/mssql/server
-
Run the Docker container:
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Your_password123' -p 1433:1433 --name sqlserver -d mcr.microsoft.com/mssql/server
-
Connect to the SQL Server:
- Use the connection string:
Server=localhost,1433;Database=YourDatabase;User Id=sa;Password=Your_password123;
- Replace
YourDatabase
with the name of your database.
- Use the connection string:
For more information on using Docker with SQL Server, you can refer to the official Docker documentation and the SQL Server on Docker guide.
Apply Migrations
In the Package Manager Console within Visual Studio, run Update-Database
to apply migrations to your database.
Locate the Database
In your SQL client, expand the 'Databases' folder. You should see a list of all databases on the server.
Look for the database name specified in your ASP.NET project's connection string. If the Update-Database
command executed successfully, your database should be listed there.
Use the user authentication features provided by ASP.NET Identity to register a new user and log in to the application.
Refresh the database view, navigate to the users table (often named AspNetUsers), and verify that your new user account is listed.
Once logged in, perform CRUD operations as an authenticated user, reflecting a more realistic use-case scenario.
For any questions or suggestions, please open an issue in the repository.
This project is licensed under the MIT License. See the LICENSE file for details.