Can not instantiate proxy of class: Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a method in my repo that calls datacontext.Add method and return resutl.Entity like:



 var result =  _dataContext.Product.Add(product);
await _dataContext.SaveChangesAsync();
return result.Entity;


Now I want to create mock for EntityEntry<Product> but I am getting an exception:




Message: Castle.DynamicProxy.InvalidProxyConstructorArgumentsException
: Can not instantiate proxy of class:
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1[[Product,
Product.Entities, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]. Could not find a parameterless constructor.




Here is my Test Method code:



 var productMock = new Mock<EntityEntry<Product>>();
var entity = new Product{Id = 1, Name = "Bag"};
mappingMock.Setup(m => m.Entity).Returns(entity);
var dataContextMock = new Mock<DataContext>(_options);
var productMockSet = new Mock<DbSet<Product>>();
dataContextMock.Setup(a => a.Product)
.Returns(productMockSet.Object);
dataContextMock.Setup(m => m.Product.Add(It.IsAny<Product>())).Returns(productMock.Object);


What am I doing wrong? or is there any other way to Assert EntityEntry?










share|improve this question























  • That class has no parameterless constructor as stated by the error docs.microsoft.com/en-us/ef/core/api/…

    – Nkosi
    Nov 16 '18 at 13:56













  • Note also This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

    – Nkosi
    Nov 16 '18 at 13:57











  • Trying to mock EF can be task. Consider using in-memory database

    – Nkosi
    Nov 16 '18 at 13:58






  • 1





    What am I doing wrong? You are trying to integration test an external framework/library. That's not your task, EF Core has been tested by Microsoft. You should test against your own code. For integration tests, use in-memory as Nkosi said. For Unit Tests, better abstract your code. You should mock your repository, not EF Core in unit tests

    – Tseng
    Nov 16 '18 at 14:18











  • Then how can I Assert my method?

    – Ask
    Nov 19 '18 at 9:33


















0















I have a method in my repo that calls datacontext.Add method and return resutl.Entity like:



 var result =  _dataContext.Product.Add(product);
await _dataContext.SaveChangesAsync();
return result.Entity;


Now I want to create mock for EntityEntry<Product> but I am getting an exception:




Message: Castle.DynamicProxy.InvalidProxyConstructorArgumentsException
: Can not instantiate proxy of class:
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1[[Product,
Product.Entities, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]. Could not find a parameterless constructor.




Here is my Test Method code:



 var productMock = new Mock<EntityEntry<Product>>();
var entity = new Product{Id = 1, Name = "Bag"};
mappingMock.Setup(m => m.Entity).Returns(entity);
var dataContextMock = new Mock<DataContext>(_options);
var productMockSet = new Mock<DbSet<Product>>();
dataContextMock.Setup(a => a.Product)
.Returns(productMockSet.Object);
dataContextMock.Setup(m => m.Product.Add(It.IsAny<Product>())).Returns(productMock.Object);


What am I doing wrong? or is there any other way to Assert EntityEntry?










share|improve this question























  • That class has no parameterless constructor as stated by the error docs.microsoft.com/en-us/ef/core/api/…

    – Nkosi
    Nov 16 '18 at 13:56













  • Note also This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

    – Nkosi
    Nov 16 '18 at 13:57











  • Trying to mock EF can be task. Consider using in-memory database

    – Nkosi
    Nov 16 '18 at 13:58






  • 1





    What am I doing wrong? You are trying to integration test an external framework/library. That's not your task, EF Core has been tested by Microsoft. You should test against your own code. For integration tests, use in-memory as Nkosi said. For Unit Tests, better abstract your code. You should mock your repository, not EF Core in unit tests

    – Tseng
    Nov 16 '18 at 14:18











  • Then how can I Assert my method?

    – Ask
    Nov 19 '18 at 9:33














0












0








0


1






I have a method in my repo that calls datacontext.Add method and return resutl.Entity like:



 var result =  _dataContext.Product.Add(product);
await _dataContext.SaveChangesAsync();
return result.Entity;


Now I want to create mock for EntityEntry<Product> but I am getting an exception:




Message: Castle.DynamicProxy.InvalidProxyConstructorArgumentsException
: Can not instantiate proxy of class:
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1[[Product,
Product.Entities, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]. Could not find a parameterless constructor.




Here is my Test Method code:



 var productMock = new Mock<EntityEntry<Product>>();
var entity = new Product{Id = 1, Name = "Bag"};
mappingMock.Setup(m => m.Entity).Returns(entity);
var dataContextMock = new Mock<DataContext>(_options);
var productMockSet = new Mock<DbSet<Product>>();
dataContextMock.Setup(a => a.Product)
.Returns(productMockSet.Object);
dataContextMock.Setup(m => m.Product.Add(It.IsAny<Product>())).Returns(productMock.Object);


What am I doing wrong? or is there any other way to Assert EntityEntry?










share|improve this question














I have a method in my repo that calls datacontext.Add method and return resutl.Entity like:



 var result =  _dataContext.Product.Add(product);
await _dataContext.SaveChangesAsync();
return result.Entity;


Now I want to create mock for EntityEntry<Product> but I am getting an exception:




Message: Castle.DynamicProxy.InvalidProxyConstructorArgumentsException
: Can not instantiate proxy of class:
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1[[Product,
Product.Entities, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]. Could not find a parameterless constructor.




Here is my Test Method code:



 var productMock = new Mock<EntityEntry<Product>>();
var entity = new Product{Id = 1, Name = "Bag"};
mappingMock.Setup(m => m.Entity).Returns(entity);
var dataContextMock = new Mock<DataContext>(_options);
var productMockSet = new Mock<DbSet<Product>>();
dataContextMock.Setup(a => a.Product)
.Returns(productMockSet.Object);
dataContextMock.Setup(m => m.Product.Add(It.IsAny<Product>())).Returns(productMock.Object);


What am I doing wrong? or is there any other way to Assert EntityEntry?







.net unit-testing asp.net-core moq






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 13:53









AskAsk

306315




306315













  • That class has no parameterless constructor as stated by the error docs.microsoft.com/en-us/ef/core/api/…

    – Nkosi
    Nov 16 '18 at 13:56













  • Note also This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

    – Nkosi
    Nov 16 '18 at 13:57











  • Trying to mock EF can be task. Consider using in-memory database

    – Nkosi
    Nov 16 '18 at 13:58






  • 1





    What am I doing wrong? You are trying to integration test an external framework/library. That's not your task, EF Core has been tested by Microsoft. You should test against your own code. For integration tests, use in-memory as Nkosi said. For Unit Tests, better abstract your code. You should mock your repository, not EF Core in unit tests

    – Tseng
    Nov 16 '18 at 14:18











  • Then how can I Assert my method?

    – Ask
    Nov 19 '18 at 9:33



















  • That class has no parameterless constructor as stated by the error docs.microsoft.com/en-us/ef/core/api/…

    – Nkosi
    Nov 16 '18 at 13:56













  • Note also This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

    – Nkosi
    Nov 16 '18 at 13:57











  • Trying to mock EF can be task. Consider using in-memory database

    – Nkosi
    Nov 16 '18 at 13:58






  • 1





    What am I doing wrong? You are trying to integration test an external framework/library. That's not your task, EF Core has been tested by Microsoft. You should test against your own code. For integration tests, use in-memory as Nkosi said. For Unit Tests, better abstract your code. You should mock your repository, not EF Core in unit tests

    – Tseng
    Nov 16 '18 at 14:18











  • Then how can I Assert my method?

    – Ask
    Nov 19 '18 at 9:33

















That class has no parameterless constructor as stated by the error docs.microsoft.com/en-us/ef/core/api/…

– Nkosi
Nov 16 '18 at 13:56







That class has no parameterless constructor as stated by the error docs.microsoft.com/en-us/ef/core/api/…

– Nkosi
Nov 16 '18 at 13:56















Note also This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

– Nkosi
Nov 16 '18 at 13:57





Note also This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

– Nkosi
Nov 16 '18 at 13:57













Trying to mock EF can be task. Consider using in-memory database

– Nkosi
Nov 16 '18 at 13:58





Trying to mock EF can be task. Consider using in-memory database

– Nkosi
Nov 16 '18 at 13:58




1




1





What am I doing wrong? You are trying to integration test an external framework/library. That's not your task, EF Core has been tested by Microsoft. You should test against your own code. For integration tests, use in-memory as Nkosi said. For Unit Tests, better abstract your code. You should mock your repository, not EF Core in unit tests

– Tseng
Nov 16 '18 at 14:18





What am I doing wrong? You are trying to integration test an external framework/library. That's not your task, EF Core has been tested by Microsoft. You should test against your own code. For integration tests, use in-memory as Nkosi said. For Unit Tests, better abstract your code. You should mock your repository, not EF Core in unit tests

– Tseng
Nov 16 '18 at 14:18













Then how can I Assert my method?

– Ask
Nov 19 '18 at 9:33





Then how can I Assert my method?

– Ask
Nov 19 '18 at 9:33












1 Answer
1






active

oldest

votes


















0














I think you are missing these mocking objects:



var iStateManager = new Mock<IStateManager>();
var model = new Mock<Model>();

var productEntityEntry = new Mock<EntityEntry<Product>>(
new InternalShadowEntityEntry(iStateManager.Object, new EntityType("Product", model.Object, ConfigurationSource.Convention)));

productEntityEntry.SetupGet(m=> m.Entity).Returns(entity);





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339226%2fcan-not-instantiate-proxy-of-class-microsoft-entityframeworkcore-changetracking%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I think you are missing these mocking objects:



    var iStateManager = new Mock<IStateManager>();
    var model = new Mock<Model>();

    var productEntityEntry = new Mock<EntityEntry<Product>>(
    new InternalShadowEntityEntry(iStateManager.Object, new EntityType("Product", model.Object, ConfigurationSource.Convention)));

    productEntityEntry.SetupGet(m=> m.Entity).Returns(entity);





    share|improve this answer




























      0














      I think you are missing these mocking objects:



      var iStateManager = new Mock<IStateManager>();
      var model = new Mock<Model>();

      var productEntityEntry = new Mock<EntityEntry<Product>>(
      new InternalShadowEntityEntry(iStateManager.Object, new EntityType("Product", model.Object, ConfigurationSource.Convention)));

      productEntityEntry.SetupGet(m=> m.Entity).Returns(entity);





      share|improve this answer


























        0












        0








        0







        I think you are missing these mocking objects:



        var iStateManager = new Mock<IStateManager>();
        var model = new Mock<Model>();

        var productEntityEntry = new Mock<EntityEntry<Product>>(
        new InternalShadowEntityEntry(iStateManager.Object, new EntityType("Product", model.Object, ConfigurationSource.Convention)));

        productEntityEntry.SetupGet(m=> m.Entity).Returns(entity);





        share|improve this answer













        I think you are missing these mocking objects:



        var iStateManager = new Mock<IStateManager>();
        var model = new Mock<Model>();

        var productEntityEntry = new Mock<EntityEntry<Product>>(
        new InternalShadowEntityEntry(iStateManager.Object, new EntityType("Product", model.Object, ConfigurationSource.Convention)));

        productEntityEntry.SetupGet(m=> m.Entity).Returns(entity);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 15 at 10:18









        KarinaKarina

        1




        1
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339226%2fcan-not-instantiate-proxy-of-class-microsoft-entityframeworkcore-changetracking%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python