Today, We are going to cover xUnit integration testing with an In Memory database in Asp.Net Core.
I assumed you already work with Asp.Net core application so we are not going to discuss about asp.net core and xUnit.
You can find the source code from my Github repository.
Source code, I have created asp.net core application and one class library. Class library for integration testing purpose created.
Lets see step by step
Step 1: If you have already asp.net core application than create new project (Class Library) for integration testing
Step 2: Once class library created, require to following dependencies on project.json file
Also add "testRunner" on project.json file.
Step 3: Once all dependency added than its look like following
Step 5: Create new class file, in our example we are create UserTest.cs
Step 6: For In Memory Database, we are going setup on constructor level and pass options to Context.
Step 7: Time to write test cases, we have written following sample test case for user
Hope you like it and visit my Github Repository for source code
I assumed you already work with Asp.Net core application so we are not going to discuss about asp.net core and xUnit.
You can find the source code from my Github repository.
Source code, I have created asp.net core application and one class library. Class library for integration testing purpose created.
Lets see step by step
Step 1: If you have already asp.net core application than create new project (Class Library) for integration testing
Step 2: Once class library created, require to following dependencies on project.json file
"Microsoft.EntityFrameworkCore.InMemory": "1.1.0", "xunit": "2.2.0-beta2-build3300", "dotnet-test-xunit": "2.2.0-preview2-build1029",
Also add "testRunner" on project.json file.
Step 3: Once all dependency added than its look like following
{ "version": "1.0.0-*", "testRunner": "xunit", "dependencies": { "Microsoft.EntityFrameworkCore.InMemory": "1.1.0", "xunit": "2.2.0-beta2-build3300", "dotnet-test-xunit": "2.2.0-preview2-build1029", "NETStandard.Library": "1.6.0", "InMemoryDatabaseAspNetCore.Model": "1.0.0-*" }, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" } } } } }Step 4: Now, all set regarding configuration so we looking for code.
Step 5: Create new class file, in our example we are create UserTest.cs
Step 6: For In Memory Database, we are going setup on constructor level and pass options to Context.
private InMemoryContext _context; public UserTest() { var builder = new DbContextOptionsBuilder<InMemoryContext>(); builder.UseInMemoryDatabase(); var options = builder.Options; _context = new InMemoryContext(options); }Here you can check we have use InMemoeryData with builder builder.UseInMemoryDatabase()
Step 7: Time to write test cases, we have written following sample test case for user
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using InMemoryDatabaseAspNetCore.Model; using Microsoft.EntityFrameworkCore; using Xunit; namespace InMemory.Test { public class UserTest { private InMemoryContext _context; public UserTest() { var builder = new DbContextOptionsBuilder<InMemoryContext>(); builder.UseInMemoryDatabase(); var options = builder.Options; _context = new InMemoryContext(options); } [Fact] public void RunTests() { AddUser(); GetAllUser(); UpdateUser(); } private void AddUser() { // Act var user = new ApplicationUser() { Email = "satasiya.kalpesh2006@gmail.com", Birthday = new DateTime(1988, 10, 21), Gender = "Male", Name = "Kalpesh Satasiya", UserName = "kalpeshsatasiya", }; _context.Users.Add(user); var newUser = _context.SaveChanges(); // Assert Assert.NotNull(newUser); } private void GetAllUser() { var users = _context.Users.Select(x=>x).ToList(); Assert.NotNull(users); Assert.True(users.Any(), "Not record found"); } private void UpdateUser() { var users = _context.Users.Select(x => x).ToList(); if (users.Any()) { users[0].UserName = "KalpeshSatasiyaTest"; users[0].Email = "satasiya.kalpesh2006Test@gmail.com"; users[0].Name = "Kalpesh Satasiya"; users[0].Country = "India"; _context.Users.Update(users[0]); var updateUser = _context.SaveChanges(); Assert.NotNull(updateUser); } } } }Step 8: Run test cases.
Hope you like it and visit my Github Repository for source code
0 comments:
Post a Comment