Click here to Skip to main content
13,091,145 members (51,161 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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:
        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();
        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
Posted 25-Feb-13 21:05pm
Updated 26-Feb-13 18:06pm
SleepyCrat 26-Feb-13 23:33pm
This is just a a question, but in your customer object that you are creating at first you are not setting the field 'Id', but you are setting it in the customer object you are creating in the CreateList() function. Also in the comparison statement you are using to get the 'filter' object
the second part of your statement is "l.Id != customer.Id'. Did you perhaps mean to set the Id field the same in both objects and use 'l.Id == customer.Id'?
sarahnimy 27-Feb-13 0:05am
sorry typing mistake might have removed it while using ctrl+Z...sorry..

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


I was breaking my head over it for the past hour and finally found it. :)

U just have to create a default.aspx page in your web service project..!

following is the link that should solve your doubt...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 9 Feb 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100