Why does this test fail? How do you add and retrieve items from a mock repository?
[TestMethod]
public void Can_Mock_Sales_Force_Repo()
{
// arrange
var salesForcePolicyRepo = MockRepository.GenerateMock<IRepository<SalesForcePolicy>>();
// act
var salesForcePolicy = new SalesForcePolicy { PolicyNumber = "123456" };
IEnumerable<SalesForcePolicy> salesForcePolicies = new List<SalesForcePolicy> { salesForcePolicy };
salesForcePolicyRepo.Stub(x => x.FindByExp(null)).IgnoreArguments().Return(salesForcePolicies.AsQueryable());
// assert
Assert.IsTrue(salesForcePolicyRepo.Contains(salesForcePolicy));
}
The FindByExp
method is in the base interface.
public interface IRepository<T> : IReadOnlyRepository<T> where T : class
{
void EnrollInUnitOfWork(IUnitOfWork unitOfWork);
IQueryable<T> FindBy(Func<T, bool> predicate);
IQueryable<T> FindBy(Func<T, bool> predicate, params Expression<Func<T, object>>[] includes);
void Add(T item);
void Add(IEnumerable<T> items);
void Remove(T item);
void Remove(IEnumerable<T> items);
void Update(T item);
int Execute(string command, params object[] parameters);
IEnumerable<T> ExecWithStoreProcedure(string query, params object[] parameters);
void Attach(T item);
}
public interface IReadOnlyRepository<T> where T : class
{
int Count { get; }
bool Contains(T item);
IQueryable<T> FindAll();
IQueryable<T> FindAll(params Expression<Func<T, object>>[] includes);
IQueryable<T> FindByExp(Expression<Func<T, bool>> predicate);
IQueryable<T> FindByExp(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes);
}
Aucun commentaire:
Enregistrer un commentaire