Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing EF 4.0 with POCO and t4 templates - How mock context?

I'm trying to create fake context accodring to http://blogs.msdn.com/b/adonet/archive/2009/12/17/walkthrough-test-driven-development-with-the-entity-framework-4-0.aspx

As i can see there is an interface which exposes methods which returns IObjectSet<...>, but T4 templates generates methods which returns ObjectSet<...> and there is no generated interface and on that page author adds interface to created context and it gives him way to create mock etc.

My main goal is to use T4 templates to generate poco classes and create mock/fake context to test my custom repositories. Is there any way to make it works without writing or changing T4 template?? How can i create mocks above context (for IObjectSet is't trivial) if it's returning ObjectSet's instead of IObjectSets...

Thx in advance

like image 522
Simon Avatar asked Sep 01 '10 12:09

Simon


1 Answers

The author is just mocking the repository, not the entities. EntityFramework generate ObjectQueries, but he wraps them and his repository returns IObjectQueries. He does this so he can easily mock the data, then during the save he just validates the entities.

If you are just trying to create a "mock" repository you can create your own T4 template and iterate over the edmx file and generate the code. But there is no reason to have to generate POCOS? They already exist, why do you need to recreate them? He abstracted everything into a "generic" FakeObjectSet so there really isn't that much code to write?

Are you trying to generate this:

   public IObjectSet<Blog> Blogs
    {
        get
        {
            return _blogs ?? (_blogs = new FakeObjectSet<Blog>());
        }
        set
        {
            _blogs = value as FakeObjectSet<Blog>;
        }
    }
    private FakeObjectSet<Blog> _blogs;

If so I am going to guess you are going to spend more time with T4 then you would just writing it.


Example T4 without the class declaration... you can do the complete t4 by following this blog

<#
    foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
    {
#>
public IObjectSet<<#=MultiSchemaEscape(set.ElementType, code)#>>
{
    get{
       return <#=code.FieldName(set)#> ??  ( <#=code.FieldName(set)#> = FakeObjectSet<<#=MultiSchemaEscape(set.ElementType, code)#>>("<#=set.Name#>"));
    }
    set{
   <#=code.FieldName(set)#>  = value as FakeObjectSet<<#=MultiSchemaEscape(set.ElementType, code)#>>("<#=set.Name#>");
    }
}
 private FakeObjectSet<<#=MultiSchemaEscape(set.ElementType, code)#>> <#=code.FieldName(set)#>;
<#
 }

#>

Which would generate this code:

public IObjectSet<Blogs>{
  get{
     return _Blogs??  ( _Blogs = FakeObjectSet<Blog>("Blogs"));
   }
  set{
    _Blogs= value as FakeObjectSet<Class>("Blogs");
  }
}

private FakeObjectSet<Blog> _Blogs;

Side note.

IObjectSet is contained in System.Data so Add a reference to System.Data.Entity.dll

like image 103
Nix Avatar answered Oct 18 '22 01:10

Nix