Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: UnitTesting MVC4
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
Posted 25-Feb-13 22:05pm
Edited 26-Feb-13 19:06pm
v7
Comments
SleepyCrat at 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 at 27-Feb-13 0:05am
   
sorry typing mistake might have removed it while using ctrl+Z...sorry..

1 solution

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

Solution 1

Hi
 
I was breaking my head over it for the past hour and finally found it. Smile | :)
 
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
  Permalink  
v2

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

  Print Answers RSS
0 Maciej Los 290
1 OriginalGriff 275
2 Sergey Alexandrovich Kryukov 195
3 BillWoodruff 140
4 ProgramFOX 140
0 OriginalGriff 6,524
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,218
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 9 Feb 2014
Copyright © CodeProject, 1999-2014
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