0

I have the following repository interface

public interface IRepository<TEntity> {

    TEntity FindById(int id);
    TEntity FindById(long id);
     etc
}

I then have the following Repository class that inherits from the interface above

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class {
        private DB _context;

        public Repository() {
            _context = new DB();
        }

        public Repository(DB context) {
            _context = context;
        }

        Methods.....
}

I have created a FakeRepository<> that also inherits from IRepository<>

public class FakeRepository<TEntity> : IRepository<TEntity> where TEntity : class {
        private FakeDataContext _context;

        public FakeRepository() {
            _context = new FakeDataContext();
        }

        public FakeRepository(FakeDataContext context) {
            _context = context;
        }

        Methods....
}

The only difference between the Repository and the Fake is that rather than using the LinqToSQL DBContext I have created a fake one for testing purposes.

What I then what to be able to do in my unit tests is:

ObjectFactory.Initialize(x => { x.For(typeof(Repository<>)).Use(typeof(FakeRepository<>)); });

So my tests will always be run against the in-memory fakedbcontext.

However I get structuremap error 104 pluggin type error and i'm not sure why?

Gaz
  • 1,249
  • 3
  • 19
  • 37

2 Answers2

1

In your Initialize you should map IRepository against FakeRepository. Not Repository against FakeRepository. Then in your tests when you ask for an instance of IRepository you will get the fake one.

ObjectFactory.Initialize(x => { x.For(typeof(IRepository<>)).Use(typeof(FakeRepository<>)); });
Bassetassen
  • 20,852
  • 8
  • 39
  • 40
  • The only problem with that is I create concrete repositories like this public class UserRepository : Repository so that won't work – Gaz Aug 15 '11 at 09:04
  • If UserRepository derives from Repository, it will always derive from Repository. StructureMap cannot change which type your classes derive from. It can only swap out dependencies. – Joshua Flanagan Aug 16 '11 at 21:17
0

Why you don't use Dev Magic Fake, it has Fake the repository with many features like, save any type retrieve any type with Id, it can save the type permanent through serialization the memory, it also can generate data for your types and many other features for more information you can see the following link:

http://devmagicfake.codeplex.com/

Thanks

M.Radwan

Mohamed.Radwan -MVP
  • 2,724
  • 2
  • 16
  • 24