Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: LINQ UnitTesting MVC4
i have a service lyk below:
public class SubjectService : ISubjectService
    {        
        private readonly ISubjectRepository subjectRepository;
        public SubjectService(ISubjectRepository subjectRepository)
        {
            this.broadSubjectAreaRepository = broadSubjectAreaRepository;
        }
        public IQueryable<Subject> GetSubjectByFilter(Expression<Func<Subject, bool>> where)
        {
            var subject = this.subjectRepository.GetMany(where);
            return subject;
        }
    }
the GetMany is defined in the repository implementation like this-
public virtual IQueryable<T> GetMany(Expression<Func<T, bool>> where)
    {       
        return dbset.Where(where);
    }
i had written a unit test for the service like this-
[TestMethod()]
        public void GetByFilter_ShouldReturn_ListofSubject_AfterApplyingFilters()
        {
            var subject = new Subject()
            {
                Id = 3,
                Name = "Physics",
                SubjectCode = "PHY",
                StatusId = 1
            };
 
            var list = CreateList();
            var mockrepository = new Mock<ISubjectRepository>();
            mockrepository.Setup(p => p.GetMany( l => l.Name.Trim().ToUpper() == subject.Name.Trim().ToUpper() && l.Id != subject.Id)).Returns(list.AsQueryable());
      
            var service = new SubjectService(mockrepository.Object); 
 
            var filter = service.GetSubjectByFilter( l => l.Name.Trim().ToUpper() == subject.Name.Trim().ToUpper() && l.Id != subject.Id).ToList();
 
            Assert.AreEqual(1,filter.Count());
        }   
 
        private static List<Subject> CreateList()
        {
            var list = new List<Subject>()
            {
           new Subject{Id = 1,Name ="Physics", SubjectCode ="PHY", StatusId=1},
           new Subject{Id = 2, Name ="Chemistry", SubjectCode ="CHEM", StatusId =1},
           new Subject{Id = 3, Name ="English", SubjectCode ="ENG", StatusId=0}                
            };
            return list;
        }
can anyone verify the test let me know if whether i am going wrong anywhere???
Posted 23-Feb-13 0:03am
Edited 6-Mar-13 18:44pm
v4

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

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 174
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 6 Mar 2013
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