Skip to content

seesharpdev/EntityFramework.Testing

Repository files navigation

EntityFramework.Testing NuGet VersionBuild status

EntityFramework.Testing provides an implementation of DbAsyncQueryProvider that can be used when testing a component that uses async queries with EntityFramework.

The project is cut from EntityFrameworks' source code. Some changes are made to be compliant with StyleCop/CodeAnalysis

All the following mocked DbSet operations work as expected:

  • Add
  • AddRange
  • AsNoTracking
  • Attach
  • Create
  • Find
  • FindAsync
  • Include
  • Remove
  • RemoveRange

The following samples are based on this Controller:

publicclassBlogsController:Controller{privatereadonlyBloggingContextdb;publicBlogsController(BloggingContextcontext){db=context;}publicasyncTask<ViewResult>Index(){varquery=db.Blogs.OrderBy(b =>b.Name);returnView(awaitquery.ToListAsync());}}

EntityFramework.Testing.Moq NuGet Version

EntityFramework.Testing.Moq provides a helpful extension method to mock EntityFramework's DbSets using Moq.

You can write a unit test against a mock context as follows. SetupData extension method is part of EntityFramework.Testing.Moq.

[TestMethod]publicasyncTaskIndex_returns_blogs_ordered_by_name(){// Create some test datavardata=newList<Blog>{newBlog{Name="BBB"},newBlog{Name="CCC"},newBlog{Name="AAA"}};// Create a mock set and contextvarset=newMock<DbSet<Blog>>().SetupData(data);varcontext=newMock<BloggingContext>();context.Setup(c =>c.Blogs).Returns(set.Object);// Create a BlogsController and invoke the Index actionvarcontroller=newBlogsController(context.Object);varresult=awaitcontroller.Index();// Check the resultsvarblogs=(List<Blog>)result.Model;Assert.AreEqual(3,blogs.Count());Assert.AreEqual("AAA",blogs[0].Name);Assert.AreEqual("BBB",blogs[1].Name);Assert.AreEqual("CCC",blogs[2].Name);}

EntityFramework.Testing.Moq.Ninject NuGet Version

EntityFramework.Testing.Moq.Ninject provides a Ninject Module to auto mock DbContext and its DbSet<> properties using Ninject.MockingKernel.Moq.

[TestMethod]publicasyncTaskIndex_returns_blogs_ordered_by_name(){using(varkernel=newMoqMockingKernel()){kernel.Load(newEntityFrameworkTestingMoqModule());// Create some test datavardata=newList<Blog>{newBlog{Name="BBB"},newBlog{Name="CCC"},newBlog{Name="AAA"}};// Setup mock setkernel.GetMock<DbSet<Blog>>().SetupData(data);// Get a BlogsController and invoke the Index actionvarcontroller=kernel.Get<BlogsController>();varresult=awaitcontroller.Index();// Check the resultsvarblogs=(List<Blog>)result.Model;Assert.AreEqual(3,blogs.Count());Assert.AreEqual("AAA",blogs[0].Name);Assert.AreEqual("BBB",blogs[1].Name);Assert.AreEqual("CCC",blogs[2].Name);}}

EntityFramework.Testing.NSubstitute NuGet Version

EntityFramework.Testing.NSubstitute provides a helpful extension method to mock EntityFramework's DbSets using NSubstitute.

You can write a unit test against a mock context as follows. SetupData extension method is part of EntityFramework.Testing.NSubstitute.

[TestMethod]publicasyncTaskIndex_returns_blogs_ordered_by_name(){// Create some test datavardata=newList<Blog>{newBlog{Name="BBB"},newBlog{Name="CCC"},newBlog{Name="AAA"}};// Create a DbSet substitute.varset=Substitute.For<DbSet<Blog>,IQueryable<Blog>,IDbAsyncEnumerable<Blog>>().SetupData(data);varcontext=Substitute.For<BloggingContext>();context.Blogs.Returns(set);// Create a BlogsController and invoke the Index actionvarcontroller=newBlogsController(context);varresult=awaitcontroller.Index();// Check the resultsvarblogs=(List<Blog>)result.Model;Assert.AreEqual(3,blogs.Count());Assert.AreEqual("AAA",blogs[0].Name);Assert.AreEqual("BBB",blogs[1].Name);Assert.AreEqual("CCC",blogs[2].Name);}

EntityFramework.Testing.NSubstitute.Ninject NuGet Version

EntityFramework.Testing.NSubstitute.Ninject provides a Ninject Module to auto mock DbContext and its DbSet<> properties using Ninject.MockingKernel.NSubstitute.

[TestMethod]publicasyncTaskIndex_returns_blogs_ordered_by_name(){using(varkernel=newNSubstituteMockingKernel()){kernel.Load(newEntityFrameworkTestingNSubstituteModule());// Create some test datavardata=newList<Blog>{newBlog{Name="BBB"},newBlog{Name="CCC"},newBlog{Name="AAA"}};// Setup mock setkernel.Get<DbSet<Blog>>().SetupData(data);// Get a BlogsController and invoke the Index actionvarcontroller=kernel.Get<BlogsController>();varresult=awaitcontroller.Index();// Check the resultsvarblogs=(List<Blog>)result.Model;Assert.AreEqual(3,blogs.Count());Assert.AreEqual("AAA",blogs[0].Name);Assert.AreEqual("BBB",blogs[1].Name);Assert.AreEqual("CCC",blogs[2].Name);}}

EntityFramework.Testing.FakeItEasy NuGet Version

EntityFramework.Testing.FakeItEasy provides a helpful extension method to mock EntityFramework's DbSets using FakeItEasy.

You can write a unit test against a mock context as follows. SetupData extension method is part of EntityFramework.Testing.FakeItEasy.

[TestMethod]publicasyncTaskIndex_returns_blogs_ordered_by_name(){// Create some test datavardata=newList<Blog>{newBlog{Name="BBB"},newBlog{Name="CCC"},newBlog{Name="AAA"}};// Create a DbSet substitute.varset=A.Fake<DbSet<Blog>>(o =>o.Implements(typeof(IQueryable<Blog>)).Implements(typeof(IDbAsyncEnumerable<Blog>))).SetupData(data);varcontext=A.Fake<BloggingContext>();context.Blogs.Returns(set);// Create a BlogsController and invoke the Index actionvarcontroller=newBlogsController(context);varresult=awaitcontroller.Index();// Check the resultsvarblogs=(List<Blog>)result.Model;Assert.AreEqual(3,blogs.Count());Assert.AreEqual("AAA",blogs[0].Name);Assert.AreEqual("BBB",blogs[1].Name);Assert.AreEqual("CCC",blogs[2].Name);}

EntityFramework.Testing.FakeItEasy.Ninject NuGet Version

EntityFramework.Testing.FakeItEasy.Ninject provides a Ninject Module to auto mock DbContext and its DbSet<> properties using Ninject.MockingKernel.FakeItEasy.

[TestMethod]publicasyncTaskIndex_returns_blogs_ordered_by_name(){using(varkernel=newFakeItEasyMockingKernel()){kernel.Load(newEntityFrameworkTestingFakeItEasyModule());// Create some test datavardata=newList<Blog>{newBlog{Name="BBB"},newBlog{Name="CCC"},newBlog{Name="AAA"}};// Setup mock setkernel.Get<DbSet<Blog>>().SetupData(data);// Get a BlogsController and invoke the Index actionvarcontroller=kernel.Get<BlogsController>();varresult=awaitcontroller.Index();// Check the resultsvarblogs=(List<Blog>)result.Model;Assert.AreEqual(3,blogs.Count());Assert.AreEqual("AAA",blogs[0].Name);Assert.AreEqual("BBB",blogs[1].Name);Assert.AreEqual("CCC",blogs[2].Name);}}

About

EntityFramework Testing

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C#100.0%