3
Ich verwende Entity Framework 7 in .net Core 1.0 rc2. Hier ist die Klasse.Wie matcht man dbcontext?
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Blog> Blogs { get; set; }
}
dann die ApplicationDbContext
zu einer Klasse
public class BtnValidator
{
private readonly ApplicationDbContext _dbContext;
public BtnValidator(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
}
Nicht sicher injizieren, wie es in Unit-Test-Methode zu verspotten.
[Fact]
public void Ensure_Proper_Btn_Validated_Return_True()
{
var dbContext = mockup(ApplicationDbContext); //how
var validator = new BtnValidator(dbContext);
var results = validator.IsValid("1234");
Assure.True(results);
}
EDIT
In BtnValidator
habe ich Code die dbContext
zuzugreifen.
public IsValid(string ID)
{
var results = _dbContext.Blogs.First(x => x.ID);
//
}
Es ist kein Mock, sondern prüfen, mit in- Speicheranbieter für EF (Aufwand - https://effort.codeplex.com/). Gut für Unit-Test EF-Code. – Evk
@Evk, es gibt zwei Dinge. Eine ist, ich benötige Unit-Test, Zweitens verwende ich .Net-Core 1.0 RC2, die Bibliothek vielleicht nicht unterstützt. –
Vielleicht ist es an dieser Stelle nicht praktisch für Sie, aber überlegen Sie sich, ein Repository-Muster zu verwenden. Dann verspotten Sie das Repo, nicht den tatsächlichen Datenbankzugriff. – Crowcoder