GithubHelp home page GithubHelp logo

isabella232 / justmock.entityframework Goto Github PK

View Code? Open in Web Editor NEW

This project forked from telerik/justmock.entityframework

0.0 0.0 0.0 486 KB

In-memory mock DbSet and DbContext mocking amenities for JustMock.

License: Apache License 2.0

C# 100.00%

justmock.entityframework's Introduction

Telerik.JustMock.EntityFramework

In-memory mock DbSet and DbContext mocking amenities for JustMock.

Getting Started

Install the package from NuGet: https://www.nuget.org/packages/JustMock.EntityFramework

Mocking DbContext

Here is an example straight from the tests:

[TestClass]
public class DbContextTests
{
	public class Person
	{
		public int Id { get; set; }
		public string Name { get; set; }
	}

	public class DbContextOne : DbContext
	{
		public DbSet<Person> People { get; set; }
	}

	[TestMethod]
	public void Prepare_ShouldAssignDbSetProperties()
	{
		var dbContext = EntityFrameworkMock.Create<DbContextOne>();
		Assert.IsNotNull(dbContext.People);
	}
}

In the above example I create a mock of my DbContext-deriving class. As a result, all fields that are of type DbSet<T> or IDbSet<T> are automatically populated with a mock in-memory DbSet (initially empty).

We can also create a mock from a DbContext-like interface, like in the example below:

public interface IMyDbContext
{
	IDbSet<Person> People { get; }
}

[TestMethod]
public void Prepare_Interface_DbSetsInitialized()
{
	var dbContext = EntityFrameworkMock.Create<IMyDbContext>();
	Assert.IsNotNull(dbContext.People);
}

Populating data

Populating the context with data lets you simulate a database that has data in it. Here's how we can populate our context:

[TestMethod]
public async Task Bind_BindData_DataIsThere()
{
	var ctx = Mock.Create<TheDbContext>().PrepareMock();

	var list = new List<Person>
	{
		new Person { Id = 1, Name = "a" }
	};

	ctx.People.Bind(list);

	var data = await ctx.People.ToListAsync();
	Assert.AreSame(list[0], data[0]);
}

The Bind() extension method binds a DbSet or IDbSet on the mock context to a backing collection, which can be anything, but it's best to use a ObservableCollection<T> (Local works only when the backing collection is itself observable).

Changes made to the DbSet are passed to the backing collection and vice versa.

Further reading

Check out the unit tests in Telerik.JustMock.EntityFramework.Tests - they double as example usage.

justmock.entityframework's People

Contributors

mihail-vladov 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.