Giter VIP home page Giter VIP logo

Comments (12)

gloinho avatar gloinho commented on June 12, 2024 2

Hey, I cant work this around... Something changed?

from entityframework-classic.

JonathanMagnan avatar JonathanMagnan commented on June 12, 2024 1

Hello @pilarodriguez ,

Thank you for reporting, we will look at it today.

Best Regards,

Jonathan

from entityframework-classic.

JonathanMagnan avatar JonathanMagnan commented on June 12, 2024 1

Hello @pilarodriguez ,

That one has been a lot hard then we initially thought but we finally make it compatible with Moq.

The v7.0.24 has been released.

Let me know if everything works as expected.

If you need, we can also provide an example that works with Include.

Best Regards,

Jonathan

from entityframework-classic.

JonathanMagnan avatar JonathanMagnan commented on June 12, 2024 1

We will look at it today

from entityframework-classic.

pilarodriguez avatar pilarodriguez commented on June 12, 2024

Thank you. Yes, it would be great it you could provide me some examples for Include

from entityframework-classic.

JonathanMagnan avatar JonathanMagnan commented on June 12, 2024

Sure, you should get it later today.

from entityframework-classic.

JonathanMagnan avatar JonathanMagnan commented on June 12, 2024

Hello @pilarodriguez ,

Here is an example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Castle.Core.Logging;
using Moq;

namespace Z.Lab.Request_Mock
{
    public partial class Form_Request_QueryFilter_Interface : Form
    {
        public static int ApplicationTenantID = 1;

        public Form_Request_QueryFilter_Interface()
        {
            var customer = new Customer();
            var order = new Order() { Name = "blabla" };
            customer.Orders.Add(order);

            var customers = new List<Customer>
            {
                customer
            }.AsQueryable();

            var orders = new List<Order>
            {
                order
            }.AsQueryable();

            var mockContext = new Mock<EntityContext>(MockBehavior.Loose);

            var mockCustomersSet = new Mock<DbSet<Customer>>();
            mockCustomersSet.As<IQueryable<Customer>>().Setup(m => m.Provider).Returns(customers.Provider);
            mockCustomersSet.As<IQueryable<Customer>>().Setup(m => m.Expression).Returns(customers.Expression);
            mockCustomersSet.As<IQueryable<Customer>>().Setup(m => m.ElementType).Returns(customers.ElementType);
            mockCustomersSet.As<IQueryable<Customer>>().Setup(m => m.GetEnumerator()).Returns(customers.GetEnumerator());
            mockCustomersSet.Setup(m => m.Include("Orders")).Returns(mockCustomersSet.Object);

            var mockOrdersSet = new Mock<DbSet<Order>>();
            mockOrdersSet.As<IQueryable<Order>>().Setup(m => m.Provider).Returns(orders.Provider);
            mockOrdersSet.As<IQueryable<Order>>().Setup(m => m.Expression).Returns(orders.Expression);
            mockOrdersSet.As<IQueryable<Order>>().Setup(m => m.ElementType).Returns(orders.ElementType);
            mockOrdersSet.As<IQueryable<Order>>().Setup(m => m.GetEnumerator()).Returns(orders.GetEnumerator());

            mockContext.SetupGet(c => c.Customers).Returns(mockCustomersSet.Object);
            mockContext.SetupGet(c => c.Orders).Returns(mockOrdersSet.Object);

            var customerList = mockContext.Object.Customers.Include("Orders").Select(p => p).ToList();
        }

        public class EntityContext : DbContext
        {
            public EntityContext() : base(My.ConnectionString)
            {
            }

            public virtual DbSet<Customer> Customers { get; set; }
            public virtual DbSet<Order> Orders { get; set; }

        }

        public class Customer
        {
            public Customer()
            {
                Orders = new List<Order>();
            }

            public int CustomerID { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public Boolean IsDeleted { get; set; }
            public virtual List<Order> Orders { get; set; }
        }

        public class Order
        {
            public int OrderID { get; set; }
            public int CustomerID { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public Boolean IsDeleted { get; set; }
        }
    }
}

Let me know if you need more help on this issue.

Best Regards,

Jonathan

from entityframework-classic.

pilarodriguez avatar pilarodriguez commented on June 12, 2024

Hello @JonathanMagnan . Sorry, I was having some other issues with my code and I couldn't try the new package version.

The problem I have with your example is that we are not using dbContext.Customers.Include("Orders"). We are using dbContext.Customers.Include(s => s.Orders), which uses the public IncludeDbQuery<TResult, TProperty> Include<TProperty>(Expression<Func<TResult, TProperty>> path);.
So I cannot mock it with mockCustomersSet.Setup(m => m.Include("Orders")).Returns(mockCustomersSet.Object);

from entityframework-classic.

JonathanMagnan avatar JonathanMagnan commented on June 12, 2024

Hello @pilarodriguez ,

My developer tried it and it seems to works with both syntax.

We tried it with var customerList = mockContext.Object.Customers.Include(p => p.Orders).Select(p => p).ToList(); as you use and everything work.

Are we missing something in your last comment?

from entityframework-classic.

pilarodriguez avatar pilarodriguez commented on June 12, 2024

I finally have it working. The problem is that instead of var customerList = mockContext.Object.Customers.Include(p => p.Orders).Select(p => p).ToList();, I was trying something like var customerList = mockContext.Object.Customers.Include(p => p.Orders).ToList(); (without the Select), so I was getting an exception.
I'm not sure why I have a problem if I just do a ToList after the Include

from entityframework-classic.

JonathanMagnan avatar JonathanMagnan commented on June 12, 2024

Hello @pilarodriguez ,

We tried with the following code:

var customerList = mockContext.Object.Customers.Include("Orders").ToList();

And that still work for us, the Select was only here by mistake.

If everything works, could we close this request?

Best Regards,

Jonathan

from entityframework-classic.

pilarodriguez avatar pilarodriguez commented on June 12, 2024

Sure. Thanks a lot for your support

from entityframework-classic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.