English 中文(简体)
How to write a Mock Repository to test WCF RIA Domain Service build on top of Entity Framework Context
原标题:

I need to write a test layer to Test my WCF RIA Domain Service layer which is build on top of Entity Framework context. I have come across some patterns which suggest to use a repository and then use the Domain Service factory to intilize the domain service with a repository instance to use. One of the sample which fits the requirement is explained here on Vijay s blog(http://blogs.msdn.com/vijayu/archive/2009/06/08/unit-testing-business-logic-in-net-ria-services.aspx). The problem with this implementation is that it initilize the repository only for a specific Domain Object e.g. Customer/Product but it provides no way to create a repository which can return any object which i would like to return.

Please suggest what is the right way of doing this and whether it is possible or not.

Thanks in advance,

Manoj

问题回答

I got around this issue by extending the sample with a RepositoryCollection object, which automatically instantiates LinqToSqlRepositories as needed, and also allows the insertion of mock/stub repositories manually for unit testing.

public class RepositoryCollection : IDisposable
{
    private Dictionary<Type, object> _repositories = new Dictionary<Type, object>();
    private DataContext _context;

    public RepositoryCollection() { }

    public RepositoryCollection(DataContext context)
    {
        _context = context;
    }

    public IRepository<T> Get<T>() where T : class
    {
        if(!_repositories.ContainsKey(typeof(T)))
            _repositories.Add(typeof(T), new LinqToSqlRepository<T>(_context));

        return _repositories[typeof(T)] as IRepository<T>;
    }

    public RepositoryCollection Insert<T>(IRepository<T> repository) where T : class
    {
        _repositories[typeof(T)] = repository;
        return this;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public void SubmitChanges()
    {
        if (_context != null)
            _context.SubmitChanges();
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if(_context != null)
                _context.Dispose();
        }
    }
}

Then, in your domain service, you use it like so:

private RepositoryCollection _repositoryCollection;

public MyDomainService(RepositoryCollection repositoryCollection = null)
{
    _repositoryCollection = repositoryCollection ?? new RepositoryCollection(new MyDataContext());
}

public IQueryable<Customer> GetCustomers()
{
    return _repositoryCollection.Get<Customer>().Query();
}

public IQueryable<Product> GetProducts()
{
    return _repositoryCollection.Get<Product>().Query();
}

.. other methods go here ...




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签