hello i was unit testing my service layer :
I have a service layer named "CustomerService"
public interface ICustomerService
{
IQueryable<Customer> GetCustomerByFilter(Expression<Func<Customer, bool>> where);
}
public class CustomerService : ICustomerService
{
private IRepository<Customer> customerRepository;
public CustomerService(ICustomerService rep)
{
customerRepository = rep;
}
public Customer IQueryable<Customer> GetCustomerByFilter(Expression<Func<Customer, bool>> where);
{
var customer = customerRepository.Get(Where));
if (customer == null)
return null;
return customer;
}
}
And the repository class is below:
public interface IRepository<T> : IDisposable where T : class
{
T Get(Expression<Func<T, bool>> predicate);
}
public class Repository<T> : IRepository<T> where T : class
{
private ObjectContext context;
private IObjectSet<T> objectSet;
public Repository()
: this(new demonEntities())
{
}
public Repository(ObjectContext ctx)
{
context = ctx;
objectSet = context.CreateObjectSet<T>();
}
public T Get(Expression<Func<T, bool>> predicate)
{
T entity = objectSet.Where<T>(predicate).FirstOrDefault();
if (entity == null)
return null;
return objectSet.Where<T>(predicate).FirstOrDefault();
}
i wrote a unit test as follows:
[TestMethod()]
public void GetByFilter_ShouldReturn_ListofBroadSubjectArea_AfterAplyingFilters()
{
var customer = new Customer()
{
Id = 3
Name = "Person1",
StatusId = 1
};
var list = CreateList();
var mockrepository = new Mock<ICustomerRepository>();
mockrepository.Setup(p => p.GetAll()).Returns(list.AsQueryable());
var service = new CustomerService(mockrepository.Object);
var filter = service.GetCustomerByFilter( l => l.Name.Trim().ToUpper() == customer.Name.Trim().ToUpper() && l.Id != customer.Id).ToList();
Assert.AreEqual(1,filter.Count());
}
private static List<Customer> CreateList()
{
var list = new List<Customer>()
{
new Customer{Id = 1,Name ="Person1"StatusId=1},
};
return list;
}
ohk!! my function should check the "customer " to the list created using Mock...but its not...Assert should give a true(indicating that the same record exists) in this condition but apparently its not.... Please help me in this