Cannot Implement NOP.DATA.Test project for new entity

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi,

I am using nopcommer 2.20 version. I have attached a new module for customer support. I have inserted data in the database. But when I try to implement the test the data from nop.data.test project I cannot test for my new entity(table). but the previous table/entity used in the project work fine.  var fromDb = SaveAndLoadEntity(customerSupport);

the fromDb returns always null for my table/entiry. If anyone help me regarding this issue I will be grateful to you.



Regards,

Asraful
12 years ago
mdasraful.islam wrote:
Hi,

I am using nopcommer 2.20 version. I have attached a new module for customer support. I have inserted data in the database. But when I try to implement the test the data from nop.data.test project I cannot test for my new entity(table). but the previous table/entity used in the project work fine.  var fromDb = SaveAndLoadEntity(customerSupport);

the fromDb returns always null for my table/entiry. If anyone help me regarding this issue I will be grateful to you.



Regards,

Asraful


Could you provide some more code so we can get more context around the problem? I believe the issues is that you're not using the correct database context class. You probably have a custom entity context and the tests are configured to use the nop context.
12 years ago
Thanks for your reply. Here is the code I am using
using System;
using System.Linq;
using Nop.Core.Domain.Affiliates;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Localization;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Tax;
using Nop.Tests;
using NUnit.Framework;

namespace Nop.Data.Tests.Customers
{
    [TestFixture]
    public class CustomerSupportPersistanceTest : PersistenceTest
    {
        [Test]
        public void Can_save_and_load_customersupport()
        {
           var customerSupport = GetTestCustomerSupport();
           var fromDb = SaveAndLoadEntity(customerSupport);                  

        

           fromDb.ShouldNotBeNull();

           fromDb.Title.ShouldEqual("aaa");
           fromDb.Description.ShouldEqual("bbbbb");
           fromDb.CustomerId.ShouldEqual(1);
           fromDb.Status.ShouldEqual(0);
           fromDb.InputTime.ShouldEqual(new DateTime(2010, 01, 01));
        }

        protected CustomerSupport GetTestCustomerSupport()
        {
            return new CustomerSupport
            {
                Id=1,
                Title="aaa",
                Description="bbbbb",
                CustomerId=1,
                Status=0,
                InputTime=new DateTime(2010, 01, 01)
            };
        }
              
    }
}


I am using the default dbcontext that nopcommerce tested for their module.
12 years ago
Here is the nop dbcontext I am using.

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using Nop.Core;
using NUnit.Framework;

namespace Nop.Data.Tests
{
    [TestFixture]
    public abstract class PersistenceTest
    {
        protected NopObjectContext context;

        [SetUp]
        public void SetUp()
        {
            Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
            context = new NopObjectContext(GetTestDbName());
            context.Database.Delete();
            context.Database.Create();
        }

        protected string GetTestDbName()
        {
            string testDbName = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + @"\\Nop.Data.Tests.Db.sdf;Persist Security Info=False";
            return testDbName;
        }        
        
        /// <summary>
        /// Persistance test helper
        /// </summary>
        /// <typeparam name="T">Entity type</typeparam>
        /// <param name="entity">Entity</param>
        /// <param name="disposeContext">A value indicating whether to dispose context</param>
        protected T SaveAndLoadEntity<T>(T entity, bool disposeContext = true) where T : BaseEntity
        {
            context.Set<T>().Add(entity);
            context.SaveChanges();

            object id = entity.Id;

            if (disposeContext)
            {
                context.Dispose();
                context = new NopObjectContext(GetTestDbName());
            }

            var fromDb = context.Set<T>().Find(id);
            return fromDb;
        }
    }
}


Should I used this ? Actually I added a table in the database that is customer support.
12 years ago
The error given by nunit is

Nop.Data.Tests.Customers.CustomerSupportPersistanceTest.Can_save_and_load_customersupport:
  Expected: not null
  But was:  null
12 years ago
mdasraful.islam wrote:
The error given by nunit is

Nop.Data.Tests.Customers.CustomerSupportPersistanceTest.Can_save_and_load_customersupport:
  Expected: not null
  But was:  null


If you look at the code for PersistenceTest you'll see that it creates a new instance of NopObjectContext. Your test will not work. You need to create a different version of PersistanceTest that is specific to your new object context (I'm assuming you have a different object context, but you didn't provide the code in previous posts).
12 years ago
Can you please let me help regarding this issue? I have different business object. I have added a new table custoersupport in the database. So How I can made different version of persistance test.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.