开发者

fake DataRepository - emulating the database

开发者 https://www.devze.com 2023-04-12 14:23 出处:网络
Quick Info: I\'m using C# 4.0 and RhinoMocks (with AAA) I\'ll explain with some code what I\'m thinking about doing:

Quick Info: I'm using C# 4.0 and RhinoMocks (with AAA)

I'll explain with some code what I'm thinking about doing:

public class SampleData
{
    private List<Person> _persons = new List<Person>()
    {
         new Person { PersonID = 1, Name = "Jack"},
         new Person { PersonID = 2, Name = "John"}
    };

    public List<Person> Persons
    {
        get { return _persons; }
    }
}

So this is a class which mimics data in the DB. Now I want to use this data in my unit tests. In other words, instead of getting data out of the DB, I want to get them out of the datarepository.

I think I can achieve this by stubbing the Repository and by making it use the DataRepository instead:

UC1003_ConsultantsBeherenBL consultantsBeherenBL = new UC1003_Consult开发者_高级运维antsBeherenBL();

consultantsBeherenBL = MockRepository.GeneratePartialMock<UC1003_ConsultantsBeherenBL>();
consultantsBeherenBL.Repository = MockRepository.GenerateMock<IRepository>();

This would cause my code to automaticly look for data in the DataRepository instead. So instead of stubbing a method and directly inserting a list (e.g. d => d.Find(Arg.Is.Anything)).IgnoreArguments().Return(a list which you just filled up)) I'd get "real" data back (the data which has been filtered from the DataRepository). This means I can test if my code can really find something, without having to insert test data in my DB (integration test).

How would I go about implementing such a thing? I've tried looking on the web for articles or questions, but I can't seem to find a lot :/

Any help is appreciated.

EDIT: I've tried to SimpleInjector and StructureMap, but I'm stuck implementing one of them.

I'm currently using a repository on my entity framework, so my baseBL looks like this (note: all my other BL's enherit from this one):

public class BaseBL
{
    private IRepository _repository;

    public IRepository Repository
    {
        get
        {
            if (_repository == null)
                _repository = new Repository(new DetacheringenEntities());
            return _repository;
        }
        set { _repository = value; }
    }

    public IEnumerable<T> GetAll<T>()
    {
    ... --> Generic methods

My Repository class:

public class Repository : BaseRepository, IRepository
{
    #region Base Implementation

    private bool _disposed;

    public Repository(DetacheringenEntities context)
    {
        this._context = context;
        this._contextReused = true;
    }

    #endregion

    #region IRepository Members

    public int Add<T>(T entity)
    ... --> implementations of generic methods

As far as I can make out, I now need to be able to say in my tests that instead of using the DetacheringenEntities, I need to use my DataRepository. I don't understand how I switch out my entity framework with a data class, because that data class won't fit in there.

Should I let my DataRepository enherit my IRepository class and make my own implementations?

public class SampleData : IRepository

But I can't do things like this with my lists :/

    public IEnumerable<T> GetAll<T>()
    {
        return Repository.GetAll<T>();
    }

Big thanks again for help

EDIT: I realised that a unit test doesn't need a data repository, so I'm just testing that logic in an integration test. This makes a data repository useless, since the code can be tested without a repository. I'd like to thank everybody for their help though, thanks :)


Use a Dependency injection framework to handle your dependencies. In your unit test you can swap the real implementation with a stubbed one.

In StructureMap for example, you'll say in your code. "All right, now give me the active instance of IDataRepository", for your normal code this would point to an implementation to the real database. In your unittest you can then overwrite this by putting ObjectFactory.Inject(new FakeDataRepository()). The fake repo is then used by all your code, which makes it really easy to test a single unit of work.Í

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号