Giter Club home page Giter Club logo

dbcontextmockforunittests's Introduction

DbContext Data Mocking for Unit Testing

This project demonstrates how a couple of extension methods in addition to the code posted in an article/how to named Testing with Your Own Test Doubles (EF6 onwards) make unit testing code that makes use of the Entity Framework very easy. This github project focuses on data retrieval, not adding or manipulating data although this might be added in the future.

This project also makes use of NSubstitute as the substitution/mock framework. Really any framework could be used, for an example of Moq see the original code referenced in the above link.

Structure

There are 4 library files that can be found in folder MockHelpers. Copy and include all 4 files to your Test project.

  • If you are using NSubstitute these can be copied directly from this repository without changes necessary.
  • If you are using Moq or another mocking framework you will have to change the extension methods found in class MockExtension in file MockExtension.cs as these are NSubstitute specific.

Unit tests

A demonstration can be found in class DbSetTests in the root folder in file DbSetTests.cs. There are 2 unit tests.

  • Test SynchronousTest1 demonstrates a synchronous call to a DbSet contained on a DbContext.
  • Test AsynchronousTest1 demonstrates an asynchronous call to a DbSet contained on a DbContext. Again, both tests make use of NSubstitute but you could plugin your preferred Mocking framework with minimal effort.

Both tests attach the data directly to the generic Set'1 method but really any type of DbSet property or method could be subsittuted.

Extension Methods

These extension methods do all the work for you.

// these 2 methods are the easiest to use and are extension methods on DbContext
AddToDbSetForAsync
AddToDbSet

// These 2 methods are to generate a mocked/substituted DbSet<TEntity> instance from any IEnumerable<TEntity>
GenerateMockDbSet
GenerateMockDbSetForAsync

Examples

Example 1

[TestMethod]
public async Task AsynchronousTest1()
{
	// arrange
	var data = new List<MyEntity>()
	{
		new MyEntity() {Id = 1, Name = "Entity A", Relations = new List<MyRelatedPropertyEntity>() {new MyRelatedPropertyEntity {Id = 1, Name = "one"}}},
		new MyEntity() {Id = 2, Name = "Entity B", Relations = new List<MyRelatedPropertyEntity>() {new MyRelatedPropertyEntity {Id = 54, Name = "two"}}},
		new MyEntity() {Id = 3, Name = "Entity C", Relations = new List<MyRelatedPropertyEntity>() {new MyRelatedPropertyEntity {Id = 51, Name = "three"}}},
		new MyEntity() {Id = 4, Name = "Entity D", Relations = new List<MyRelatedPropertyEntity>() {new MyRelatedPropertyEntity {Id = 321, Name = "four"}}}
	};
	
	// create dbContext - this could be any derived class from DbContext
	var dbContext = NSubstitute.Substitute.For<DbContext>();

	// assign my mock data set using the extension method
	dbContext.AddToDbSetForAsync(data);

	// include the relations property
	dbContext.Set<MyEntity>().MockInclude(x => x.Relations);

	// act - use await and call ToListAsync. Typically this would be executed from inside some other Type that is being tested
	var result = await dbContext.Set<MyEntity>().Where(x => x.Id > 2).OrderBy(x => x.Id).ToListAsync();

	// assert
	Assert.AreEqual(2, result.Count);
	CollectionAssert.AreEqual(new int[] { 3, 4 }, result.Select(x => x.Id).ToArray());
}

dbcontextmockforunittests's People

Contributors

igorwolbers avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.