GithubHelp home page GithubHelp logo

stasiko / ef-testable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jkodroff/ef-testable

0.0 1.0 0.0 483 KB

Wrappers for Entity Framework to Make It More Testable

License: MIT License

JavaScript 15.76% C# 84.24%

ef-testable's Introduction

EF Testable

Wrappers for Entity Framework to Make It More Testable

EF Testable provides 2 interfaces:

  1. IDataSession, a read/write wrapper around DB context. This is the only interface most applications will need.
  2. IReadOnlyDataSession, a read-only optimized wrapper around DB context. All Set<T>() calls are .AsNoTracking() for better performance and SaveChanges() is not exposed to avoid any confusing behavior. Use this interface if you have a class that only needs to read data, e.g. in CQRS architecures.

If you are using both interfaces in a single class, you probably have a Separation of Concerns issue. Just sayin'.

The library is grouped into 2 NuGet packages:

  1. EntityFrameworkTestable, which you should reference in your "regular" assemblies.
  2. EntityFrameworkTestable.Testing, which you should reference in your testing assemblies. This assembly provides the DataHelper class, which makes testing EF simpler.

Somewhat Contrived Sample Code

Add some rules to your DI container. This example uses StructureMap, a fine DI container:

// This means one instance "per-HTTP request":
For<IReadOnlyDataSession>().HybridHttpOrThreadLocalScoped().Use<ReadOnlyEntityDataSession>();

// This means one instance for every class which consumes it.  Possibly a sub-optimal strategy (author is not sure just yet):
For<IDataSession>().Use<EntityDataSession>();

For<System.Data.Entity.DbContext>().Use<YourAppDbContext>();

Given this controller:

using EntityFrameworkTestable;


public class SomeController {
  private IDataSession _session;

  public SomeController(IDataSession session) {
    _session = session;
  }

  public ActionResult Index(Guid id) {
    var model = _session.Set<Something>().SingleOrDefault(x => x.Id == id);

    return model == null
      ? HttpNotFound() as ActionResult;
      : View(model);
  }
}

It could be tested like this (using FluentAssertions and NUnit in this example):

using EntityFrameworkTestable.Testing;
using FluentAssertions;

[TestFixture]
public class SomeControllerTests {
  
  [Test]
  public void Index_BadId_ReturnsHttpNotFound() {
    // Any Set<T>()'s consumed need to be explicitly defined.
    // This is gentle "encouragement" to keep the number of table
    // dependencies low in your code.
    var data = DataHelper.AddSet<Something(); 
    
    new SomeController(data);
      .Index(Guid.NewGuid())
      .Should().BeOfType<HttpNotFound>()
  }

  [Test]
  public void Index_ModelFound_ReturnsView() {
    var id = Guid.NewGuid();

    var data = DataHelper
      .Session(new Something {
        Id = id
      }); // You can tack on additional calls to AddSet() if you need to mock other Set<T>()'s
    
    new SomeController(data);
      .Index(id)
      .Should().BeOfType<ViewResult>()
  }
}

ef-testable's People

Contributors

jkodroff avatar

Watchers

 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.