Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock System.Data.Linq.Table<MyClass>

One of my base repository classes contains a method:

public abstract class RepositoryBase<T, TDb> : IRepository<T>
    where T : IEntity
    where TDb : class, IDbEntity, new()
{
    protected internal abstract Table<TDb> GetTable();
    ...
}

I am writing unit test for derived repository class that contains implementation of mentioned method:

public class CmOptionRepository : 
    RepositoryBase<ICmOption, CMCoreDAL.DbData.CMOption>, ICmOptionRepository
{
    protected internal override System.Data.Linq.Table<CMCoreDAL.DbData.CMOption>
        GetTable()
    {
        return Context.CMOptions;
    }

....
}

Here: Context - is Linq-model of DB, CMOptions - one of the DB tables.

I want my 'GetTable()' method returning a special set of data.

I am going to mock the method:

        System.Data.Linq.Table<CMCoreDAL.DbData.CMOption> table = ...;
        Mock<CmOptionRepository> mockRepository =
            new Mock<CmOptionRepository>(MockBehavior.Strict);
        mockRepository.Setup(mock => mock.GetTable()).Returns(table);

But don't know how to create an instance of System.Data.Linq.Table<CMCoreDAL.DbData.CMOption> class.

Question: how can I mock the System.Data.Linq.Table<>? Or probably I need to change method signature in order to avoid System.Data.Linq.Table<> class usage?

Please advise. Any thoughts are welcome.

P.S. I am using Moq.

like image 269
Budda Avatar asked Nov 22 '10 18:11

Budda


2 Answers

If you are using .NET 4.0, Table<T> implements ITable<T> so that you should use the interface ITable<TDb> in the return type of GetTable instead of the concrete type. Then you can mock away.

In .NET 3.5 it is a little more difficult because Table<T> only implements ITable (not generic).

like image 171
jason Avatar answered Oct 08 '22 09:10

jason


You shouldn't really expose Table<T> outside of your repository unless there is an explicit need to perform operations on the Table<T> instance. Instead, return IQueryable<T> in your repository, which is easier to mock. If you need to perform updates then you can return ITable<T>.

like image 23
Matthew Abbott Avatar answered Oct 08 '22 09:10

Matthew Abbott