How to solve IQueryable' doesn't implement 'IAsyncQueryProvider issue - c#

i'm trying to write a test to test that the method that gets categoryById returns the correct data but i'm getting a System.AggregateException. please does anyone have an idea how to fix this issue? thanks
Test setup:
[SetUp]
public void Setup()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<MapperInitilizer>();
});
_realMapper = new Mapper(config);
_categories = new Mock<IRepository<Category>>();
_unitOfWork = new Mock<IUnitOfWork>();
_unitOfWork.SetupGet(work => work.Categories).Returns(_categories.Object);
_logger = new Mock<ILogger<CategoryController>>();
_mapper = new Mock<IMapper>();
_categoryController = new CategoryController(_logger.Object, _mapper.Object,_unitOfWork.Object);
category_one = new Category()
{
Id = 1,
CreatedAt = new DateTime(2022, 8,8),
Description = "music desc",
Title = "music"
};
category_two = new Category()
{
Id = 2,
CreatedAt = new DateTime(2022, 9,8),
Description = "sport desc",
Title = "sport"
};
}
CategoryTestRepository's Get method
public async Task<Category> Get(Expression<Func<Category, bool>> expression, Func<IQueryable<Category>, IIncludableQueryable<Category, object>> include = null)
{
IQueryable<Category> query = Source.AsQueryable();
return await query.AsNoTracking().FirstOrDefaultAsync(expression);
}
Get categoryById test
[Test]
public void GetCategory_InputCategoryId_ReturnCategoryById()
{
List<Category> categoryList = new List<Category> {category_one, category_two};
var expectedResult = categoryList[0];
var repo = new CategoryTestRepository(categoryList);
var unitOfWork = new Mock<IUnitOfWork>();
unitOfWork.SetupGet(work => work.Categories).Returns(repo);
var categoryController = new CategoryController(_logger.Object, _realMapper, unitOfWork.Object);
categoryController.GetCategory(1).GetAwaiter().GetResult();
Assert.IsNotEmpty(repo.Source);
Assert.AreEqual(expectedResult.Id, repo.Get(x => x.Id == 1).Result);
}
Error:
System.AggregateException : One or more errors occurred. (The provider for the source
'IQueryable' doesn't implement 'IAsyncQueryProvider'. Only provi...
System.AggregateException : One or more errors occurred. (The provider for the source 'IQueryable' doesn't implement 'IAsyncQueryProvider'. Only providers that implement 'IAsyncQueryProvider' can be used for Entity Framework asynchronous operations.)
----> System.InvalidOperationException : The provider for the source 'IQueryable' doesn't implement 'IAsyncQueryProvider'. Only providers that implement 'IAsyncQueryProvider' can be used for Entity Framework asynchronous operations.
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()

Related

How to unit test method with TryUpdateModelAsync invovled in ASP.NET CORE Razor Page Web App?

TryUpdateModelAsync is an internal protected method of abstract class PageModel, which makes it, to my view, unmockable. Is there any approach to unit test any of the action that involves this method?
I saw this solution on the internet:
asp.net core mvc controller unit testing when using TryUpdateModel.
However, it only applies to ASP.NET Core MVC Web App since TryUpdateModelAsync is a public method, so we can add an adapator to wrap this method and make our adaptor call the actual method. In Razor Page, this TryUpdateModelAsync is inaccessible from outside.
The problem I was having right now is when I was unit testing the OnPostAsync method, TryUpdateModelAsync always throw an exception. I could never pass through that line to check the code logic after it.
So here is the action I want to unit test:
public class CreateModel : PageModel
{
//elided
[BindProperty]
public Post Post { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
if (await TryUpdateModelAsync<Post>(Post, "Post", p => p.Title, p => p.SubTitle, p => p.Author, p => p.Content, p => p.CategoryID)){
//do stuff
return RedirectToPage("./Index");
}
//do stuff
return Page();
}
Here's the unit test I wrote for it:
[Test]
public async Task InvokeOnPostAsyncWithInvalidModelState_ShouldReturnPageResultType(){
//Arrange
InitPostModelProperties();
var createModel = new CreateModel(){
PageContext = _pageContext,
TempData = _tempData,
Url = _urlHelper
};
//do arrangement
//Act
var result = await createModel.OnPostAsync();
//Assert
}
UPDATE:
Here's the full initialization process I did for this test:
public void InitPostModelProperties(){
_httpContext = new DefaultHttpContext(){
//RequestServices = services.BuildServiceProvider()
};
_modelStateDictionary = new ModelStateDictionary();
_modelMetaDataProvider = new EmptyModelMetadataProvider();
_actionContext = new ActionContext(_httpContext, new RouteData(), new PageActionDescriptor(), _modelStateDictionary);
_viewData = new ViewDataDictionary(_modelMetaDataProvider, _modelStateDictionary);
_tempData = new TempDataDictionary(_httpContext, Mock.Of<ITempDataProvider>());
_pageContext = new PageContext(_actionContext){
ViewData = _viewData
};
_urlHelper = new UrlHelper(_actionContext);
}
Then I got following exception message:
Error Message:
System.ArgumentNullException : Value cannot be null.
Parameter name: metadataProvider
Stack Trace:
at Microsoft.AspNetCore.Mvc.ModelBinding.Internal.ModelBindingHelper.TryUpdateModelAsync(Object model, Type modelType, String prefix, ActionContext actionContext, IModelMetadataProvider metadataProvider, IModelBinderFactory modelBinderFactory, IValueProvider valueProvider, IObjectModelValidator objectModelValidator, Func`2 propertyFilter)
at Microsoft.AspNetCore.Mvc.RazorPages.PageModel.TryUpdateModelAsync[TModel](TModel model, String name, Expression`1[] includeExpressions)
Then I changed my initialization to the following:
public void InitPostModelProperties(){
var services = new ServiceCollection();
services.AddTransient<IModelMetadataProvider, ModelMetadataProvider>();
_httpContext = new DefaultHttpContext(){
RequestServices = services.BuildServiceProvider()
};
_modelStateDictionary = new ModelStateDictionary();
_modelMetaDataProvider = new EmptyModelMetadataProvider();
_actionContext = new ActionContext(_httpContext, new RouteData(), new PageActionDescriptor(), _modelStateDictionary);
_viewData = new ViewDataDictionary(_modelMetaDataProvider, _modelStateDictionary);
_tempData = new TempDataDictionary(_httpContext, Mock.Of<ITempDataProvider>());
_pageContext = new PageContext(_actionContext){
ViewData = _viewData
};
_urlHelper = new UrlHelper(_actionContext);
}
Then I got new exception message:
Outcome: Failed
Error Message:
System.InvalidOperationException : No service for type 'Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider' has been registered.
Stack Trace:
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.AspNetCore.Mvc.RazorPages.PageModel.get_MetadataProvider()
at Microsoft.AspNetCore.Mvc.RazorPages.PageModel.TryUpdateModelAsync[TModel](TModel model, String name, Expression`1[] includeExpressions)
I followed the exception message and added these lines:
services.AddSingleton<IModelMetadataProvider, EmptyModelMetadataProvider>();
Got new exception message:
Outcome: Failed
Error Message:
System.InvalidOperationException : No service for type 'Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory' has been registered.
Stack Trace:
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.AspNetCore.Mvc.RazorPages.PageModel.get_ModelBinderFactory()
at Microsoft.AspNetCore.Mvc.RazorPages.PageModel.TryUpdateModelAsync[TModel](TModel model, String name, Expression`1[] includeExpressions)
I kept adding new lines:
services.AddSingleton<IModelBinderFactory,ModelBinderFactory>();
Only to find the service can't be instansiated beause ModelBinderFactory class is an abstract class
I am stuck here.
UPDATE
The whole test code is shown below:
SRC:
public class BaseTests
{
private HugoBlogContext _context;
public HugoBlogContext Context => _context;
public List<Category> CatList;
public List<Post> PostList;
public List<Tag> TagList;
public List<PostTag> PostTagList;
public BaseTests(){}
public class PageModelBaseTests: BaseTests
{
protected ICategoryRepository _categoryRepository;
protected IPostRepository _postRepository;
protected IPostTagRepository _postTagRepository;
protected ITagRepository _tagRepository;
protected ISelectTagService _selectTagService;
protected IDropdownCategoryService _dropdownCategoryService;
protected HttpContext _httpContext;
protected ModelStateDictionary _modelStateDictionary;
protected ActionContext _actionContext;
protected ModelMetadataProvider _modelMetaDataProvider;
protected ViewDataDictionary _viewData;
protected TempDataDictionary _tempData;
protected PageContext _pageContext;
protected UrlHelper _urlHelper;
[SetUp]
public void Init(){
_categoryRepository = new CategoryRepository(Context);
_postRepository = new PostRepository(Context);
_postTagRepository = new PostTagRepository(Context);
_tagRepository = new TagRepository(Context);
_selectTagService = new SelectTagService(_tagRepository, _postRepository, _postTagRepository);
_dropdownCategoryService = new DropdownCategoryService(_categoryRepository);
}
public void InitPostModelProperties(){
var services = new ServiceCollection();
services.AddSingleton<IModelMetadataProvider, EmptyModelMetadataProvider>();
services.AddSingleton<IModelBinderFactory,ModelBinderFactory>();
_httpContext = new DefaultHttpContext(){
RequestServices = services.BuildServiceProvider()
};
_modelStateDictionary = new ModelStateDictionary();
_modelMetaDataProvider = new EmptyModelMetadataProvider();
_actionContext = new ActionContext(_httpContext, new RouteData(), new PageActionDescriptor(), _modelStateDictionary);
_viewData = new ViewDataDictionary(_modelMetaDataProvider, _modelStateDictionary);
_tempData = new TempDataDictionary(_httpContext, Mock.Of<ITempDataProvider>());
_pageContext = new PageContext(_actionContext){
ViewData = _viewData
};
_urlHelper = new UrlHelper(_actionContext);
}
[TestFixture]
public class CreateModelTests: PageModelBaseTests
{
[Test]
public async Task InvokeOnPostAsyncWithValidModelState_ShouldReturnRedirectPageResultTypeAndCategoryShouldBeUpdated(){
//Arrange
InitPostModelProperties();
var createModel = new CreateModel(_selectTagService, _dropdownCategoryService ,_postRepository){
PageContext = _pageContext,
TempData = _tempData,
Url = _urlHelper,
};
createModel.Post = new Post{
Title = "Create",
SubTitle = "Create",
Content = "Create",
Author = "Create",
CategoryID = CatList.Count + 1,
};
var selectedTags = new string[3]{"4","5","6"};
//Act
var result = await createModel.OnPostAsync(selectedTags);
//Assert
result.Should().BeOfType<RedirectToPageResult>();
Context.Posts.Where(c => c.CategoryID == (CatList.Count + 1)).Should().NotBeNull();
}
}
UPDATE
After mocking, I got the following Exception Message:
Outcome: Failed
Error Message:
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
at Microsoft.AspNetCore.Mvc.ModelBinding.Internal.ModelBindingHelper.TryUpdateModelAsync(Object model, Type modelType, String prefix, ActionContext actionContext, IModelMetadataProvider metadataProvider, IModelBinderFactory modelBinderFactory, IValueProvider valueProvider, IObjectModelValidator objectModelValidator, Func`2 propertyFilter)
at Microsoft.AspNetCore.Mvc.RazorPages.PageModel.TryUpdateModelAsync[TModel](TModel model, String name, Expression`1[] includeExpressions)
FYI: I also mocked IObjectValidator by using services.AddSingleton<IObjectValidator>(Mock.Of<IObjectValidator>())
I'm not sure it's our responsibility to test TryValidateModel or TryUpdateModelAsync; surely it is safe to presume that the framework methods are correct, right.
Another argument is that you are testing your implementation and not someone else's.
Anyhow, if you would like to ignore the methods, you can try this in your test method (and I am using Moq as my Mocking Framework) to ignore the methods completely and in effect presume they are correct:
var objectModelValidatorMock = new Mock<IObjectModelValidator>();
objectModelValidatorMock
.Setup(o => o.Validate(
It.IsAny<ActionContext>(),
It.IsAny<ValidationStateDictionary>(),
It.IsAny<string>(),
It.IsAny<object>()));
var sut = new NewController
{
ObjectValidator = objectModelValidatorMock.Object
};

How to mock DocumentClient CreateDocumentQuery AsDocumentQuery [duplicate]

From the new CosmosDb emulator I got sort of a repository to perform basic documentdb operations, this repository gets injected to other classes. I wanted to unit test a basic query.
public class DocumentDBRepository<T> where T : class
{
//Details ommited...
public IQueryable<T> GetQueryable()
{
return _client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true });
}
public async Task<IEnumerable<T>> ExecuteQueryAsync(IQueryable<T> query)
{
IDocumentQuery<T> documentQuery = query.AsDocumentQuery();
List<T> results = new List<T>();
while (documentQuery.HasMoreResults)
{
results.AddRange(await documentQuery.ExecuteNextAsync<T>());
}
return results;
}
}
This repository needs a document client to work, which also gets injected on the constructor.
public DocumentDBRepository(string databaseId, IDocumentClient client)
{
_client = client;
_databaseId = databaseId;
_collectionId = GetCollectionName();
}
My initial approach was to use the CosmosDb emulator, but that required the emulator to run which I don't like and makes the tests slower.
My second approach was to try and use a mock of the document client.
var data = new List<MyDocumentClass>
{
new MyDocumentClass{ Description= "BBB" },
new MyDocumentClass{ Description= "ZZZ" },
}
.AsQueryable()
.OrderBy(q => q.Description);
var client = new Mock<IDocumentClient>();
client.As<IDocumentClient>()
.Setup(foo => foo.CreateDocumentQuery<MyDocumentClass>(It.IsAny<Uri>(), It.IsAny<FeedOptions>()))
.Returns(data);
DocumentDBRepository<MyDocumentClass> repo= new DocumentDBRepository<MyDocumentClass>(cosmosDatabase, client.Object);
The code that uses this repository works like this:
var query = _documentsRepository.GetQueryable()
.Where(d => d.Description = description)
.OrderByDescending(d => d.description)
.Take(100);
//Execute query async fails.
var result = await _documentsRepository.ExecuteQueryAsync(query);
It fails because the repository tries to convert the IQueryable to a IDocumentQuery object, which is very specific to DocumentDb api (See method ExecuteQueryAsync above). Later on, it executes HasMoreResults method on it. So the problem is, even if I mock .asDocumentQuery() to return my object, I don't know how to provide a result for HasMoreResults and ExecuteNextAsync so that it makes sense for my unit tests.
My third option would be to straight mock my repository instead of the DocumentClient object. Would be, I think, simpler, but I wouldn't be testing much of the DocumentDb api.
The key to this is that the CreateDocumentQuery you are calling, though shown as returning IOrderedQueryable, the encapsulated result will also be derived from IDocumentQuery which is what would allow .AsDocumentQuery() to work.
Now normally you should not be mocking what you do not own. However in this case if you want to exercise ExecuteQueryAsync to completion you can create a fake abstraction that will allow the test to be exercised to completion.
The following Example shows how it can be done.
[TestClass]
public class DocumentDBRepositoryShould {
/// <summary>
/// Fake IOrderedQueryable IDocumentQuery for mocking purposes
/// </summary>
public interface IFakeDocumentQuery<T> : IDocumentQuery<T>, IOrderedQueryable<T> {
}
[TestMethod]
public async Task ExecuteQueryAsync() {
//Arrange
var description = "BBB";
var expected = new List<MyDocumentClass> {
new MyDocumentClass{ Description = description },
new MyDocumentClass{ Description = "ZZZ" },
new MyDocumentClass{ Description = "AAA" },
new MyDocumentClass{ Description = "CCC" },
};
var response = new FeedResponse<MyDocumentClass>(expected);
var mockDocumentQuery = new Mock<IFakeDocumentQuery<MyDocumentClass>>();
mockDocumentQuery
.SetupSequence(_ => _.HasMoreResults)
.Returns(true)
.Returns(false);
mockDocumentQuery
.Setup(_ => _.ExecuteNextAsync<MyDocumentClass>(It.IsAny<CancellationToken>()))
.ReturnsAsync(response);
var client = new Mock<IDocumentClient>();
client
.Setup(_ => _.CreateDocumentQuery<MyDocumentClass>(It.IsAny<Uri>(), It.IsAny<FeedOptions>()))
.Returns(mockDocumentQuery.Object);
var cosmosDatabase = string.Empty;
var documentsRepository = new DocumentDBRepository<MyDocumentClass>(cosmosDatabase, client.Object);
//Act
var query = documentsRepository.GetQueryable(); //Simple query.
var actual = await documentsRepository.ExecuteQueryAsync(query);
//Assert
actual.Should().BeEquivalentTo(expected);
}
}
Here is Nkosi's answer ported to NSubstitute:
[TestClass]
public class DocumentDBRepositoryShould
{
[TestMethod]
public async Task ExecuteQueryAsync()
{
// Arrange
var description = "BBB";
var expected = new List<MyDocumentClass> {
new MyDocumentClass{ Description = description },
new MyDocumentClass{ Description = "ZZZ" },
new MyDocumentClass{ Description = "AAA" },
new MyDocumentClass{ Description = "CCC" },
};
var response = new FeedResponse<MyDocumentClass>(expected);
var mockDocumentQuery = Substitute.For<IFakeDocumentQuery<MyDocumentClass>>();
mockDocumentQuery.HasMoreResults.Returns(true, false);
mockDocumentQuery.ExecuteNextAsync<MyDocumentClass>(Arg.Any<CancellationToken>())
.Returns(Task.FromResult(response));
var client = Substitute.For<IDocumentClient>();
client.CreateDocumentQuery<MyDocumentClass>(Arg.Any<Uri>(), Arg.Any<FeedOptions>())
.ReturnsForAnyArgs(mockDocumentQuery);
var cosmosDatabase = string.Empty;
var documentsRepository = new DocumentDBRepository<MyDocumentClass>(cosmosDatabase, client);
//Act
var actual = await documentsRepository.GetDataAsync(); //Simple query.
//Assert
actual.Should().BeEquivalentTo(expected);
}
public class MyDocumentClass
{
public string Description { get; set; }
}
public interface IFakeDocumentQuery<T> : IDocumentQuery<T>, IOrderedQueryable<T> {
}
public class DocumentDBRepository<T>
{
private readonly string cosmosDatabase;
private readonly IDocumentClient documentClient;
public DocumentDBRepository(string cosmosDatabase, IDocumentClient documentClient)
{
this.cosmosDatabase = cosmosDatabase;
this.documentClient = documentClient;
}
public async Task<IEnumerable<MyDocumentClass>> GetDataAsync()
{
var documentUri = UriFactory.CreateDocumentCollectionUri(cosmosDatabase, "test-collection");
var query = documentClient
.CreateDocumentQuery<MyDocumentClass>(documentUri)
.AsDocumentQuery();
var list = new List<MyDocumentClass>();
while (query.HasMoreResults)
{
var rules = await query.ExecuteNextAsync<MyDocumentClass>();
list.AddRange(rules);
}
return list;
}
}
}

How to (should I) mock DocumentClient for DocumentDb unit testing?

From the new CosmosDb emulator I got sort of a repository to perform basic documentdb operations, this repository gets injected to other classes. I wanted to unit test a basic query.
public class DocumentDBRepository<T> where T : class
{
//Details ommited...
public IQueryable<T> GetQueryable()
{
return _client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true });
}
public async Task<IEnumerable<T>> ExecuteQueryAsync(IQueryable<T> query)
{
IDocumentQuery<T> documentQuery = query.AsDocumentQuery();
List<T> results = new List<T>();
while (documentQuery.HasMoreResults)
{
results.AddRange(await documentQuery.ExecuteNextAsync<T>());
}
return results;
}
}
This repository needs a document client to work, which also gets injected on the constructor.
public DocumentDBRepository(string databaseId, IDocumentClient client)
{
_client = client;
_databaseId = databaseId;
_collectionId = GetCollectionName();
}
My initial approach was to use the CosmosDb emulator, but that required the emulator to run which I don't like and makes the tests slower.
My second approach was to try and use a mock of the document client.
var data = new List<MyDocumentClass>
{
new MyDocumentClass{ Description= "BBB" },
new MyDocumentClass{ Description= "ZZZ" },
}
.AsQueryable()
.OrderBy(q => q.Description);
var client = new Mock<IDocumentClient>();
client.As<IDocumentClient>()
.Setup(foo => foo.CreateDocumentQuery<MyDocumentClass>(It.IsAny<Uri>(), It.IsAny<FeedOptions>()))
.Returns(data);
DocumentDBRepository<MyDocumentClass> repo= new DocumentDBRepository<MyDocumentClass>(cosmosDatabase, client.Object);
The code that uses this repository works like this:
var query = _documentsRepository.GetQueryable()
.Where(d => d.Description = description)
.OrderByDescending(d => d.description)
.Take(100);
//Execute query async fails.
var result = await _documentsRepository.ExecuteQueryAsync(query);
It fails because the repository tries to convert the IQueryable to a IDocumentQuery object, which is very specific to DocumentDb api (See method ExecuteQueryAsync above). Later on, it executes HasMoreResults method on it. So the problem is, even if I mock .asDocumentQuery() to return my object, I don't know how to provide a result for HasMoreResults and ExecuteNextAsync so that it makes sense for my unit tests.
My third option would be to straight mock my repository instead of the DocumentClient object. Would be, I think, simpler, but I wouldn't be testing much of the DocumentDb api.
The key to this is that the CreateDocumentQuery you are calling, though shown as returning IOrderedQueryable, the encapsulated result will also be derived from IDocumentQuery which is what would allow .AsDocumentQuery() to work.
Now normally you should not be mocking what you do not own. However in this case if you want to exercise ExecuteQueryAsync to completion you can create a fake abstraction that will allow the test to be exercised to completion.
The following Example shows how it can be done.
[TestClass]
public class DocumentDBRepositoryShould {
/// <summary>
/// Fake IOrderedQueryable IDocumentQuery for mocking purposes
/// </summary>
public interface IFakeDocumentQuery<T> : IDocumentQuery<T>, IOrderedQueryable<T> {
}
[TestMethod]
public async Task ExecuteQueryAsync() {
//Arrange
var description = "BBB";
var expected = new List<MyDocumentClass> {
new MyDocumentClass{ Description = description },
new MyDocumentClass{ Description = "ZZZ" },
new MyDocumentClass{ Description = "AAA" },
new MyDocumentClass{ Description = "CCC" },
};
var response = new FeedResponse<MyDocumentClass>(expected);
var mockDocumentQuery = new Mock<IFakeDocumentQuery<MyDocumentClass>>();
mockDocumentQuery
.SetupSequence(_ => _.HasMoreResults)
.Returns(true)
.Returns(false);
mockDocumentQuery
.Setup(_ => _.ExecuteNextAsync<MyDocumentClass>(It.IsAny<CancellationToken>()))
.ReturnsAsync(response);
var client = new Mock<IDocumentClient>();
client
.Setup(_ => _.CreateDocumentQuery<MyDocumentClass>(It.IsAny<Uri>(), It.IsAny<FeedOptions>()))
.Returns(mockDocumentQuery.Object);
var cosmosDatabase = string.Empty;
var documentsRepository = new DocumentDBRepository<MyDocumentClass>(cosmosDatabase, client.Object);
//Act
var query = documentsRepository.GetQueryable(); //Simple query.
var actual = await documentsRepository.ExecuteQueryAsync(query);
//Assert
actual.Should().BeEquivalentTo(expected);
}
}
Here is Nkosi's answer ported to NSubstitute:
[TestClass]
public class DocumentDBRepositoryShould
{
[TestMethod]
public async Task ExecuteQueryAsync()
{
// Arrange
var description = "BBB";
var expected = new List<MyDocumentClass> {
new MyDocumentClass{ Description = description },
new MyDocumentClass{ Description = "ZZZ" },
new MyDocumentClass{ Description = "AAA" },
new MyDocumentClass{ Description = "CCC" },
};
var response = new FeedResponse<MyDocumentClass>(expected);
var mockDocumentQuery = Substitute.For<IFakeDocumentQuery<MyDocumentClass>>();
mockDocumentQuery.HasMoreResults.Returns(true, false);
mockDocumentQuery.ExecuteNextAsync<MyDocumentClass>(Arg.Any<CancellationToken>())
.Returns(Task.FromResult(response));
var client = Substitute.For<IDocumentClient>();
client.CreateDocumentQuery<MyDocumentClass>(Arg.Any<Uri>(), Arg.Any<FeedOptions>())
.ReturnsForAnyArgs(mockDocumentQuery);
var cosmosDatabase = string.Empty;
var documentsRepository = new DocumentDBRepository<MyDocumentClass>(cosmosDatabase, client);
//Act
var actual = await documentsRepository.GetDataAsync(); //Simple query.
//Assert
actual.Should().BeEquivalentTo(expected);
}
public class MyDocumentClass
{
public string Description { get; set; }
}
public interface IFakeDocumentQuery<T> : IDocumentQuery<T>, IOrderedQueryable<T> {
}
public class DocumentDBRepository<T>
{
private readonly string cosmosDatabase;
private readonly IDocumentClient documentClient;
public DocumentDBRepository(string cosmosDatabase, IDocumentClient documentClient)
{
this.cosmosDatabase = cosmosDatabase;
this.documentClient = documentClient;
}
public async Task<IEnumerable<MyDocumentClass>> GetDataAsync()
{
var documentUri = UriFactory.CreateDocumentCollectionUri(cosmosDatabase, "test-collection");
var query = documentClient
.CreateDocumentQuery<MyDocumentClass>(documentUri)
.AsDocumentQuery();
var list = new List<MyDocumentClass>();
while (query.HasMoreResults)
{
var rules = await query.ExecuteNextAsync<MyDocumentClass>();
list.AddRange(rules);
}
return list;
}
}
}

ASP.NET Generic method to mock EF6 DbSet

In my ASP.NET MVC5 web-app I'm using the EF (Model-first) for my database-communication. For unit-testing purposes, I created a generic method to generate Mock database sets. Unfortunately, I am unable to mock all the methods because when .Where(), .Any() and .Find() are used in my tested code, an exception is thrown. Can anyone help me on this? My code is below.
Unittest:
[TestClass()]
public class MessagingServiceTests
{
Mock<BoatstersEntitiesContainer> _mockContext;
MessagingService _service;
string _connectionId;
Guid _userId;
[TestInitialize()]
public void TestInitialize()
{
_userId = Guid.Parse("12345678-1234-1234-1234-123412344142");
_connectionId = "abc123";
// Setup entities
User user = new User { Id = _userId, CustomerId = 1 };
Customer customer = new Customer { Id = 1, User = user, FirstName = "TestFirstName" };
user.Customer = customer;
Customer boatOwnerCustomer = new Customer { Id = 2, FirstName = "BoatOwner" };
Boat boat = new Boat { Id = 1, Customer = boatOwnerCustomer, CustomerId = boatOwnerCustomer.Id };
boatOwnerCustomer.Boats.Add(boat);
// Init mocksets
var userMockSet = MockDbSet.Build(new List<User> { user });
var customerMockSet = MockDbSet.Build(new List<Customer> { customer, boatOwnerCustomer });
var conversationMockSet = MockDbSet.Build(new List<Conversation>());
var messageMockSet = MockDbSet.Build(new List<Message>());
var boatMockSet = MockDbSet.Build(new List<Boat> { boat });
var messagingHubConnectionMockSet = MockDbSet.Build(new List<MessagingHubConnection>());
// Setup mockcontext
_mockContext = new Mock<BoatstersEntitiesContainer>();
_mockContext.Setup(m => m.Users).Returns(userMockSet.Object);
_mockContext.Setup(m => m.Customers).Returns(customerMockSet.Object);
_mockContext.Setup(m => m.Conversations).Returns(conversationMockSet.Object);
_mockContext.Setup(m => m.Messages).Returns(messageMockSet.Object);
_mockContext.Setup(m => m.Boats).Returns(boatMockSet.Object);
_mockContext.Setup(m => m.MessagingHubConnections).Returns(messagingHubConnectionMockSet.Object);
// Start service
_service = new MessagingService(_mockContext.Object, _userId);
}
[TestMethod()]
public void When_PartnerConnected_IsTrue()
{
Conversation conversation = new Conversation {
Id = 1,
Boat = _mockContext.Object.Boats.First(b => b.Id.Equals(1)),
BoatId = _mockContext.Object.Boats.First(b => b.Id.Equals(1)).Id
};
conversation.Customers.Add(_mockContext.Object.Customers.First(b => b.Id.Equals(1)));
conversation.Customers.Add(_mockContext.Object.Customers.First(b => b.Id.Equals(2)));
MessagingHubConnection connection = new MessagingHubConnection
{
Id = 1,
Connected = true,
ConnectionId = "abc123",
Conversation = conversation,
ConversationId = 1,
Customer = _mockContext.Object.Customers.First(b => b.Id.Equals(2)),
CustomerId = 2
};
conversation.MessagingHubConnections.Add(connection);
_mockContext.Object.MessagingHubConnections.Add(connection);
_mockContext.Object.Conversations.Add(conversation);
var result = _service.IsPartnerConnected();
Assert.IsTrue(result);
// Clean up
_mockContext.Object.Conversations.RemoveRange(_mockContext.Object.Conversations);
_mockContext.Object.MessagingHubConnections.RemoveRange(_mockContext.Object.MessagingHubConnections);
}
}
Generic mockset creator:
public static class MockDbSet
{
public static Mock<DbSet<TEntity>> Build<TEntity>(List<TEntity> data) where TEntity : class
{
var queryable = data.AsQueryable();
var mockSet = new Mock<DbSet<TEntity>>();
mockSet.As<IQueryable<TEntity>>().Setup(m => m.Provider).Returns(queryable.Provider);
mockSet.As<IQueryable<TEntity>>().Setup(m => m.Expression).Returns(queryable.Expression);
mockSet.As<IQueryable<TEntity>>().Setup(m => m.ElementType).Returns(queryable.ElementType);
mockSet.As<IQueryable<TEntity>>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator());
mockSet.Setup(m => m.Add(It.IsAny<TEntity>())).Callback<TEntity>(data.Add);
return mockSet;
}
}
Messagingservice (which is being tested)
public class MessagingService : BaseRepository<Conversation>
{
private readonly Customer _customer;
private MessagingHubConnection _connection;
public MessagingService(BoatstersEntitiesContainer context, Guid userId) : base(context)
{
Context = context;
_customer = Context.Customers.First(c => c.User.Id == userId);
}
public bool IsPartnerConnected()
{
// Check if partner is connected
return Context.MessagingHubConnections.Any(c => c.ConversationId.Equals(_connection.ConversationId) && c.Customer.Id != _customer.Id && c.Connected);
}
}
In MessagingService.IsPartnerConnected(), the following exception is thrown:
Test Name: When_PartnerConnected_IsTrue
Test FullName: Boatsters.Sunshine.UnitTests.MessagingServiceTests.When_PartnerConnected_IsTrue
Test Source: C:\Users\Jelle\Source\Repos\Boatsters.Sunshine\Boatsters.Sunshine.UnitTests\MessagingServiceUnitTest.cs : line 94
Test Outcome: Failed
Test Duration: 0:00:00,0289022
Result StackTrace:
bij lambda_method(Closure , MessagingHubConnection )
bij System.Linq.Enumerable.Any[TSource](IEnumerable1 source, Func2 predicate)
bij lambda_method(Closure )
bij System.Linq.EnumerableExecutor1.Execute()
bij System.Linq.EnumerableQuery1.System.Linq.IQueryProvider.Execute[S](Expression expression)
bij System.Linq.Queryable.Any[TSource](IQueryable1 source, Expression1 predicate)
bij Boatsters.Services.MessagingService.IsPartnerConnected() in C:\Users\Jelle\Source\Repos\Boatsters.Sunshine\Boatsters.Services\MessagingService.cs:line 156
bij xxx.MessagingServiceTests.When_PartnerConnected_IsTrue() in C:\xxx\MessagingServiceUnitTest.cs:line 118
Result Message:
Test method
xxx.UnitTests.MessagingServiceTests.When_PartnerConnected_IsTrue threw exception:
System.NullReferenceException: Object reference not set to an instance of an object
Based on review of MessagingService, it is not immediately apparent where _connection is assigned a value. Looks like that is what is null when the method under test is called based on stack trace lambda_method(Closure , MessagingHubConnection )
Also from past experiences with Moq and DbSet<>, this line needs to updated so that multiple calls can be made to the data source.
mockSet.As<IQueryable<TEntity>>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator());
Change the .Returns(queryable.GetEnumerator()) to return a Func
mockSet.As<IQueryable<TEntity>>()
.Setup(m => m.GetEnumerator())
.Returns(() => queryable.GetEnumerator()); //<-- Note change here.
The original will return the same enumerator for every call which can only be enumerated once and may cause issues. Using the Func will allow a fresh enumerator to be return on every call to allow for multiple passes on the data source.

My service doesn't get my mocked DbContext through its constructor

I try to create a fake db context to pass it to my service to test my functions from the service. The error i get is : Object reference not set to an instance of an object. This happens because my service doesn't get the new mocked context. I know this because watched it through debugger.
this is my unit test code :
[TestMethod]
public void VerifiesIfGetPersonByNameReturnsAValidPerson()
{
//Arrange
var data = new List<Person>
{
new Person {Name = "Ana"},
new Person {Name = "Marius"},
new Person {Name = "George"},
}.AsQueryable();
var mockSet = new Mock<DbSet<Person>>();
mockSet.As<IQueryable<Person>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Person>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Person>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Person>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var mockContext = new Mock<PersonDbContext>();
mockContext.Setup(c => c.Persons).Returns(mockSet.Object);
var service = new PersonService(mockContext.Object);
var expected = "Marius";
//Act
var personDto = service.GetPersonByName("Marius");
var actual = personDto.Name;
//Assert
Assert.AreEqual(expected,actual);
}
And here is my service code :
public class PersonService : IPersonService
{
private PersonDbContext _db;
public PersonService(PersonDbContext db)
{
_db = db;
}
public PersonDto GetPersonByName(string name)
{
PersonDto personDto = Mapper.Map<PersonDto>(_db.Persons.Find(name));
return personDto;
}
...}
Why the service won't take the new context through its constructor ? Or what is the problem ? Why i keep getting null results when I execute the method GetPersonByName ?

Categories