@@ -129,6 +129,47 @@ public void TestFind()
129129 Assert . Equal ( 50 , cPagedlist . TotalCount ) ;
130130 }
131131
132+ /// <summary>
133+ /// TestFindWithIQueryable with AsNoTracking
134+ /// </summary>
135+ /// <see cref="http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-framework-and-asnotracking/"/>
136+ [ Fact ]
137+ public void TestFindWithIQueryable ( )
138+ {
139+ //arrange
140+ var mockobject = new Mock < IRepository < Customer > > ( ) ;
141+ mockobject . Setup ( r => r . Add ( It . IsAny < Customer > ( ) ) ) ;
142+
143+ var customer = new Customer ( ) { CustomerId = 1 } ;
144+ mockobject . Setup < IEnumerable < Customer > > ( r => r . Find ( c => c . CustomerId == customer . CustomerId ) )
145+ . Returns ( new List < Customer > ( ) { customer } ) ;
146+
147+ var mockPagedlist = new PagedList < Customer > ( ) { PageSize = 10 , PageIndex = 1 , TotalCount = 50 } ;
148+ for ( int i = 0 ; i <= 10 ; i ++ )
149+ {
150+ mockPagedlist . Add ( customer ) ;
151+ }
152+
153+ mockobject . Setup < IEnumerable < Customer > > ( r => r . Find < int > ( c => c . CustomerId == customer . CustomerId , c => c . CustomerId , 1 , 10 ) )
154+ . Returns ( mockPagedlist ) ;
155+
156+ var mockRepository = mockobject . Object ;
157+
158+ //act
159+ //Returns a new query where the entities returned will not be cached in the DbContext or ObjectContext.
160+ var clist = mockRepository . All ( ) . Where ( c => c . CustomerId == customer . CustomerId ) . AsNoTracking ( ) . ToPagedList ( 1 , 10 ) ;
161+ var cPagedlist = mockRepository . Find < int > ( c => c . CustomerId == customer . CustomerId , c => c . CustomerId , 1 , 10 ) ;
162+
163+ //assert
164+ Assert . NotNull ( clist ) ;
165+ Assert . True ( clist . Count ( ) > 0 ) ;
166+
167+ Assert . NotNull ( cPagedlist ) ;
168+ Assert . Equal ( 10 , cPagedlist . PageSize ) ;
169+ Assert . Equal ( 1 , cPagedlist . PageIndex ) ;
170+ Assert . Equal ( 50 , cPagedlist . TotalCount ) ;
171+ }
172+
132173 /// <summary>
133174 /// Tests the Find async
134175 /// </summary>
@@ -149,7 +190,6 @@ public async Task TestFindAsync()
149190 //act
150191 var clist = await mockRepository . FindAsync ( c => c . CustomerId == customer . CustomerId ) ;
151192
152-
153193 //assert
154194 Assert . NotNull ( clist ) ;
155195 Assert . True ( clist . Count ( ) > 0 ) ;
0 commit comments