Unit Test with Effort EF6 with Async Methods

c# effort entity-framework entity-framework-6 unit-testing

Question

For this, I'm attempting to put up unit tests. But this kind of arrangement is used in every case I've seen that truly works. This is how it will go.

//Setup DBContext
public MyContext(DbConnection connection) : base(connection, true){}
//Have some service that gets passed the context etc..
public class SomeService()
{
  public SomeService(MyContext passedContext){
      Context = passedContext;
  }
  public MyContext Context {get;set;}

  public Book GetBook(int id){
     return Context.Books.Find(id);
  }
}

However, I can't seem to figure out how to do it right without ruining everything because of how my setup is.

public class SomeService()
{
    public async Task<Book> GetBook(int id){
      using(var context = new MyContext()
      {
       return await context.FindAsync(id);
      }
   }
 }

How then can I test this out without a Context Property and without passing the context around? I can't perform it async since, according to what I've read, DBContext isn't thread safe. However, I am unable to test it with Effort until I pass all of the necessary context from Effort.

I use a using on every service method, so won't work.

1
0
2/23/2017 3:29:18 PM

Popular Answer

ZZZ_tmp
0
5/23/2017 12:32:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow