Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello i was unit testing my service layer :
I have a service layer named "CustomerService"
C#
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:
C#
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:
C#
               [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
Posted
Updated 26-Feb-13 18:06pm
v7
Comments
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'?
scarletwitch1990 27-Feb-13 0:05am    
sorry typing mistake might have removed it while using ctrl+Z...sorry..

1 solution

Hi
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...
http://msdn.microsoft.com/en-us/library/ms243399(v=vs.90).aspx
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900