Last active
May 5, 2016 18:36
-
-
Save AlbertoMonteiro/d82577173c83768d8f6bbace3ce6b41d to your computer and use it in GitHub Desktop.
MockDbContext
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install-package EntityFramework | |
install-package NSubstitute | |
install-package xunit | |
install-package xunit.runner.visualstudio |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data.Entity; | |
using System.Data.Entity.Infrastructure; | |
using System.Data.Entity.Validation; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using NSubstitute; | |
using Xunit; | |
namespace MockDbContext | |
{ | |
public class MockDbContextTests | |
{ | |
protected IMyContext MyContext; | |
protected DbSet<Entidade> DbSetEntidades; | |
public MockDbContextTests() | |
{ | |
MyContext = Substitute.For<IMyContext>(); | |
DbSetEntidades = Substitute.For<DbSet<Entidade>, IQueryable<Entidade>>(); | |
MyContext.Entidades.Returns(DbSetEntidades); | |
} | |
[Fact] | |
public void TesteDeMockDoDbContext() | |
{ | |
//Arrange | |
var lista = new List<Entidade> | |
{ | |
new Entidade { Id = 1, Nome = "Alberto" }, | |
new Entidade { Id = 2, Nome = "Marília" } | |
}; | |
DbSetEntidades.MockDbSetValues(lista.AsQueryable()); | |
//Act | |
var entidades = MyContext.Entidades.ToList(); | |
//Assert | |
Assert.Equal(2, entidades.Count); | |
} | |
} | |
public static class TestExtensions | |
{ | |
public static void MockDbSetValues<T>(this DbSet<T> dbSet, IQueryable<T> data) where T : class | |
{ | |
((IQueryable<T>)dbSet).Provider.Returns(data.Provider); | |
((IQueryable<T>)dbSet).Expression.Returns(data.Expression); | |
((IQueryable<T>)dbSet).ElementType.Returns(data.ElementType); | |
((IQueryable<T>)dbSet).GetEnumerator().Returns(data.GetEnumerator()); | |
} | |
} | |
public class Entidade | |
{ | |
public int Id { get; set; } | |
public string Nome { get; set; } | |
} | |
public interface IMyContext | |
{ | |
DbSet<Entidade> Entidades { get; set; } | |
Database Database { get; } | |
DbChangeTracker ChangeTracker { get; } | |
DbContextConfiguration Configuration { get; } | |
DbSet<T> Set<T>() where T : class; | |
DbSet Set(Type entityType); | |
int SaveChanges(); | |
Task<int> SaveChangesAsync(); | |
Task<int> SaveChangesAsync(CancellationToken cancellationToken); | |
IEnumerable<DbEntityValidationResult> GetValidationErrors(); | |
DbEntityEntry<T> Entry<T>(T entity) where T : class; | |
DbEntityEntry Entry(object entity); | |
} | |
public class MyContext : DbContext, IMyContext | |
{ | |
public DbSet<Entidade> Entidades { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment