I'm trying to unit test this simple method although I'm not sure how to stub out the where clause using moq? I've tried the following although I'm struggling to get it working!?
I want to test that Get() method returns a list of Person.
Thanks,
My Test
[Test]
public void TempTest()
{
// Arrange
var list = new List<string> {"Key"};
var mockRepository = new Mock<IBRepository>();
var bService = new BService(mockRepository.Object);
// ** I'm not sure about this line **
mockRepository.Setup(x => x.Where...);
// Act
var result = bService.Get(list);
// Assert
}
Method to test
private readonly IBRepository _repository;
public List<MyClass> Get(List<string> list)
{
var collection = new List<MyClass>();
foreach (var key in list)
{
var id = key;
collection.AddRange(_repository.Where(x => x.SomeProperty.Equals(id)));
}
return list;
}
IBRepository
public interface IBRepository : IRepository<B, int>
{
}
BRepository
public class BRepository : Repository<B, int>, IBRepository, IUnitOfWorkRepository
{
public BRepository(NHUnitOfWork<UserMapping> uow)
: base(uow)
{
}
}
IRepository
public interface IRepository<T, TId> : INoIdRepository<T>, IReadOnlyRepository<T, TId> where T : IAggregateRoot
{
TId Insert(T entity);
bool TryInsert(T entity, out TId entityId);
void DeleteById(TId id);
void DeleteByIds(IList<TId> ids);
}
Where functionality
IEnumerable<T> Where(Expression<Func<T, bool>> predicate);