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.
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).
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With