Skip to content

Commit

Permalink
Added FindWithIQueryable with AsNoTracking Test Case
Browse files Browse the repository at this point in the history
  • Loading branch information
megadotnet committed Jun 6, 2017
1 parent 7c2e2f8 commit d9bfafc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/BusinessEntities/RepositoryIQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace DataAccessObject
using IronFramework.Utility.UI;

/// <summary>
/// The repository i queryable extensions.
/// The repository IQueryable interface extensions.
/// </summary>
public static class RepositoryIQueryableExtensions
{
Expand Down
42 changes: 41 additions & 1 deletion source/UnitTest/Infrastructure/RepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,47 @@ public void TestFind()
Assert.Equal(50, cPagedlist.TotalCount);
}

/// <summary>
/// TestFindWithIQueryable with AsNoTracking
/// </summary>
/// <see cref="http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-framework-and-asnotracking/"/>
[Fact]
public void TestFindWithIQueryable()
{
//arrange
var mockobject = new Mock<IRepository<Customer>>();
mockobject.Setup(r => r.Add(It.IsAny<Customer>()));

var customer = new Customer() { CustomerId = 1 };
mockobject.Setup<IEnumerable<Customer>>(r => r.Find(c => c.CustomerId == customer.CustomerId))
.Returns(new List<Customer>() { customer });

var mockPagedlist = new PagedList<Customer>() { PageSize = 10, PageIndex = 1, TotalCount = 50 };
for (int i = 0; i <= 10; i++)
{
mockPagedlist.Add(customer);
}

mockobject.Setup<IEnumerable<Customer>>(r => r.Find<int>(c => c.CustomerId == customer.CustomerId, c => c.CustomerId, 1, 10))
.Returns(mockPagedlist);

var mockRepository = mockobject.Object;

//act
//Returns a new query where the entities returned will not be cached in the DbContext or ObjectContext.
var clist = mockRepository.All().Where(c => c.CustomerId == customer.CustomerId).AsNoTracking().ToPagedList(1, 10);
var cPagedlist = mockRepository.Find<int>(c => c.CustomerId == customer.CustomerId, c => c.CustomerId, 1, 10);

//assert
Assert.NotNull(clist);
Assert.True(clist.Count() > 0);

Assert.NotNull(cPagedlist);
Assert.Equal(10, cPagedlist.PageSize);
Assert.Equal(1, cPagedlist.PageIndex);
Assert.Equal(50, cPagedlist.TotalCount);
}

/// <summary>
/// Tests the Find async
/// </summary>
Expand All @@ -149,7 +190,6 @@ public async Task TestFindAsync()
//act
var clist = await mockRepository.FindAsync(c => c.CustomerId == customer.CustomerId);


//assert
Assert.NotNull(clist);
Assert.True(clist.Count() > 0);
Expand Down

0 comments on commit d9bfafc

Please sign in to comment.