C# Xunit Mocking Db Context (For Testing)

So in my App I use Repositories like.

 public class CommentRepository 
        AppDBContext context;

        public List Comments { get { return this.context.Comments.ToList();  }  }
        public CommentRepository(AppDBContext context)
            this.context = context;

The AppDbContext inherits DbContext (part of Entity Framework).

Then in the startup.cs in the ConfigureServices method I add
services.AddDbContext(options => options.UseSqlServer(Configuration["ConnectionStrings:Local"]));

Ok ok... So on to testing.

In my xUnit testing project, with a reference to the main project I can then do a Mock Context and do a database in memory.

Full Example

public class ComplaintTests
        public AppDBContext Context { get; set; }
        public ComplaintRepository ComplaintRepo;
        public CommentRepository CommentRepo;

        public ComplaintTests()
            /* Create a Memory Database instead of using the SQL */
            var optionsBuilder = new DbContextOptionsBuilder()
                .UseInMemoryDatabase(databaseName: "temp")
            Context = new AppDBContext(optionsBuilder);
            ComplaintRepo = new ComplaintRepository( this.Context);
            CommentRepo = new CommentRepository( this.Context);
        public void TestTesting()
            //Test that testing is working
        public void AddComplaint()
            Complaint complaint = new Complaint() { ID = 1, 
            Contents = "Test Complaint", Completed = false, Create = DateTime.Now };
            bool success = ComplaintRepo.AddComplaint(complaint);
            // Cleanup

A great way to do testing!

More info at Microsoft C# Core In Memory Testing

Using Android Device Monitor in Visual Studio to access locked folders

First you have to have your android emulator running

Then to open up the Android Device Monitor you click on the icon on the icon in visual studio

When it opens up it should look like

The Problem

When I click the icon next to data to look at the user data, I can’t see it because my device is not rooted. In order to get around this so I can verify file creation in my current development process. I force the ADB (android development bridge) to run as root.

So I open a command prompt via the toolbar and type adb root

Now when I view the folder in Android Device Monitor I can see the locked folder

Now there may be security concerns that I’m not aware of that make this a bad idea. The scenario for me that I need to do this is to verify that a file is created during my initial Android Applications activity is supposed to generate in the personal folder for the app.

ASP Core C# Entity Framework Cheat Sheet

Step 1) Create Models – example

namespace Project.Models{
	public class Product{
		public int ID { get; set;}
		public string Name { get; set;}
		public string Description { get; set;}
		public decimal Price { get; set;}
		public string Category { get; set;}


Entity framework looks for properties with ID in it and makes that the primary key.
Step 2) Create Repository – example

using System.Collections.Generic;
using System.Linq;
namespace Project.Models {
 public class ProductRepository : IProductRepository {
 private ApplicationDbContext context;

 public ProductRepository(ApplicationDbContext context) {
 context = context;
 public IQueryable Products => context.Products;

Step 3) Create a Database Connection string and add it to appsettings

 "Data": {
 "Project": {
 "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=ProjectDB;Trusted_Conne

Step 4) Add DB Context as a service

Open Startup.cs
Look for the method

public void ConfigureServices(IServiceCollection services) 

Then add

services.AddDbContext(options =>

Step 5) Disable Scope Verification

Edit Program.cs

Look for

public static IWebHost BuildWebHost(string[] args) =>

Change to

public static IWebHost BuildWebHost(string[] args) =>
 .UseDefaultServiceProvider(options =>
 options.ValidateScopes = false)

Step 6) Database Migration

Open command prompt within the project folder (that contains your .sln)

doetnet ef migrations add Initial
dotnet ef database update