Question

I'm going crazy so I got this

public class FrameworkDbTestBase : IDisposable
{
    protected readonly FrameworkDb Db;
    public FrameworkDbTestBase()
    {
        var connection = Effort.DbConnectionFactory.CreateTransient();
        Db = new FrameworkDb(connection);
    }

    public void Dispose()
    {
        Db.Dispose();
    }
}

This is mocking the ef6 with effort .. love it so I can continuously perform tests in the background while all the changes are happening against my codebase ... its great but unfortunately I need to this

public partial class FrameworkDb : DbContext
{
    public FrameworkDb() : base("DefaultConnection"){}
    public FrameworkDb(DbConnection connection): base(connection, true)
    {
        Configuration.LazyLoadingEnabled = false;
    }

    public DbSet<Site> Sites { get; set; }
...

in order to get the mocking of ef6 with effort going however structuremap insists on creating me a FrameworkDb instance with the long constructor the one with the DbConnection injection parameter so I get this:

StructureMap.StructureMapException was unhandled by user code
  HResult=-2146232832
  Message=StructureMap Exception Code:  202
No Default Instance defined for PluginFamily System.Data.Common.DbConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
  Source=StructureMap
  ErrorCode=202

Sigh! I roll my eyes up ... I want structuremap to use me the other shorter constructor so after some digging according to this post: Structure Map - I dont want to use the greediest constructor! I should change this:

For<FrameworkDb>().Use <FrameworkDb>();

to this

For<FrameworkDb>().Use(() => new FrameworkDb());

No such luck still same error ... and I dont want to remove the connection constructor else my integration test wont work anymore... So maybe it uses the connection only the construct the mapping and not actually use it in the injection itself ... no such luck ... adding this:

For<DbConnection>().Use(() => new EntityConnection("DefaultConnection"));

gives me that:

StructureMap.StructureMapException was unhandled by user code
  HResult=-2146232832
  Message=StructureMap Exception Code:  207
Internal exception while creating Instance '00fbcc4f-c5f0-4eb3-b814-9d0ba1bb8e19' of PluginType System.Data.Common.DbConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.  Check the inner exception for more details.
  Source=StructureMap
  ErrorCode=207

Well so much for that theory... ahum... solution anyone? Hellooo anyone? Sigh ...

Was it helpful?

Solution

Come on people no one? The answer is so simple ... well

var framework = new Framework();
For<FrameworkDb>().Use(() => framework);

So simple yet so elegant and something you just have to know!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top