C# NHibernate单元测试在多对一表上无法正常工作

C# NHibernate单元测试在多对一表上无法正常工作,c#,nhibernate,nunit,many-to-one,C#,Nhibernate,Nunit,Many To One,好吧,这就是我的问题。我对一个存储库进行了几个单元测试,我对所有这些测试都单独开了绿灯。然而,当我一起运行所有测试时,第一个测试成功,然后其他测试出现外键约束错误,这只发生在我有多对一关系的情况下。我尝试过使用cascade选项,并删除了第一个测试,以查看它是否破坏了测试数据,但所发生的一切是,我在第一个测试中得到了绿灯,其余的变为红色。在隔离状态下,我再次获得了所有测试的绿灯 大部分代码都基于NHForge.com上的tut StockTransactionRepository上发生错误。我已

好吧,这就是我的问题。我对一个存储库进行了几个单元测试,我对所有这些测试都单独开了绿灯。然而,当我一起运行所有测试时,第一个测试成功,然后其他测试出现外键约束错误,这只发生在我有多对一关系的情况下。我尝试过使用cascade选项,并删除了第一个测试,以查看它是否破坏了测试数据,但所发生的一切是,我在第一个测试中得到了绿灯,其余的变为红色。在隔离状态下,我再次获得了所有测试的绿灯

大部分代码都基于NHForge.com上的tut

StockTransactionRepository上发生错误。我已尝试包含相关代码,如果您需要更多,请告诉我。我怀疑这里有明显的东西

错误:

NHibernate.Exceptions.GenericADOException : could not insert: [rapidstk_base.Domain.StockTransaction#f305a5d4-9-c6e-bcdf-9d4dd16337ff][SQL: INSERT INTO StockTransaction (stkitm_StockItemToTransact, dcm_StockTransactionAmount, stkloc_StockLocation, dt_StockTransactionDate, Id) VALUES (?,?,?,?,?)]
   ---->MySql.Data.MySqlClient.MySqlException:  Cannot add or update a child row:  a foreign key constraint fails (`rpdstkdb`.`stocktransaction`, CONSTRAINT `FK37E1C2103E2BF85C` FOREIGN KEY (`stkitm_StockItemToTransact`) REFERENCES `stockitem` (`ID`))
hbm.xml代码:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                assembly="rapidstk_base"
                namespace="rapidstk_base.Domain"
                auto-import="false">
<class name="rapidstk_base.Domain.StockTransaction">
    <id name="Id">
        <generator class="guid" />
    </id>
    <many-to-one name="stkitm_StockItemToTransact" class="StockItem" cascade="all" />
    <property name="dcm_StockTransactionAmount" />
    <many-to-one name="stkloc_StockLocation" class="StockLocation" cascade="all" />
    <property name="dt_StockTransactionDate" />
</class>
</hibernate-mapping>


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                assembly="rapidstk_base"
                namespace="rapidstk_base.Domain"
                auto-import="false">
<class name="rapidstk_base.Domain.StockItem">
    <id name="Id">
        <generator class="guid" />
    </id>
        <property name="str_StockItemName" />
        <property name="str_StockItemDescription" />
        <property name="dtm_StockItemCreationDate" />
</class>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                assembly="rapidstk_base"
                namespace="rapidstk_base.Domain"
                auto-import="false">
<class name="rapidstk_base.Domain.StockLocation">
    <id name="Id">
        <generator class="guid" />
    </id>
    <property name="s_StockLocationName" />
    <property name="s_StockLocationDescription" />
</class>
</hibernate-mapping>

单元测试:

using NUnit.Framework;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate;
using System;
using System.Collections.Generic;

using rapidstk_base.Domain;
using rapidstk_base.Repositories;
using rapidstk_base;

namespace rapidstk_base_test
{
[TestFixture]
public class StockTransactionRepositoryTest
{
    private ISessionFactory _sessionFactory;
    private Configuration _configuration;

    private readonly static StockItem[] _stockitems = new[]
    {
        new StockItem { str_StockItemName = "Orgone Accumulator", str_StockItemDescription = "Social Integrator." },
        new StockItem { str_StockItemName = "Perpetual Dingle", str_StockItemDescription = "Everlasting Bliss." },
        new StockItem { str_StockItemName = "Apple", str_StockItemDescription = "Golden Delicious." },
        new StockItem { str_StockItemName = "Nepenthe", str_StockItemDescription = "The answer." },
        new StockItem { str_StockItemName = "Bobbins Gourd", str_StockItemDescription = "Follow The Gourd." },
    };

    private readonly static StockLocation[] _stocklocations = new[]
    {
        new StockLocation() { s_StockLocationName="Bay 1", s_StockLocationDescription="Upstairs next to the coffee machine."},
        new StockLocation() { s_StockLocationName="Bay 2", s_StockLocationDescription="Under the Shrubbery."},
        new StockLocation() { s_StockLocationName="Bay Watch", s_StockLocationDescription="Bouncing on the Beach."},
        new StockLocation() { s_StockLocationName="My Pocket", s_StockLocationDescription="Lintville."},
        new StockLocation() { s_StockLocationName="Secret Lair", s_StockLocationDescription="Next to the coke machine."},
    };

    private readonly StockTransaction[] _stockTransactions = new[]
    {
        new StockTransaction { stkitm_StockItemToTransact = _stockitems[0], stkloc_StockLocation = _stocklocations[0], dcm_StockTransactionAmount = 10.0M },
        new StockTransaction { stkitm_StockItemToTransact = _stockitems[1], stkloc_StockLocation = _stocklocations[1], dcm_StockTransactionAmount = -10.0M },
        new StockTransaction { stkitm_StockItemToTransact = _stockitems[2], stkloc_StockLocation = _stocklocations[2], dcm_StockTransactionAmount = 1.0M },
        new StockTransaction { stkitm_StockItemToTransact = _stockitems[3], stkloc_StockLocation = _stocklocations[3], dcm_StockTransactionAmount = 2.9M },
        new StockTransaction { stkitm_StockItemToTransact = _stockitems[4], stkloc_StockLocation = _stocklocations[4], dcm_StockTransactionAmount = 155.0M, dt_StockTransactionDate=DateTime.Parse("2011/11/30") },
        new StockTransaction { stkitm_StockItemToTransact = _stockitems[4], stkloc_StockLocation = _stocklocations[4], dcm_StockTransactionAmount = -50.0M, dt_StockTransactionDate=DateTime.Parse("2011/12/01") },
        new StockTransaction { stkitm_StockItemToTransact = _stockitems[4], stkloc_StockLocation = _stocklocations[4], dcm_StockTransactionAmount = -50.0M, dt_StockTransactionDate=DateTime.Parse("2011/12/02") },
        new StockTransaction { stkitm_StockItemToTransact = _stockitems[4], stkloc_StockLocation = _stocklocations[4], dcm_StockTransactionAmount = -50.0M, dt_StockTransactionDate=DateTime.Parse("2011/12/03") },
    };

    private void CreateInitialData()
    {
        using (ISession session = _sessionFactory.OpenSession())
        {
            using(ITransaction transaction = session.BeginTransaction())
            {
                foreach(var tr in _stockTransactions)
                {
                    session.Save(tr);
                }
                transaction.Commit();
            }
        }
    }

    [TestFixtureSetUp]
    public void TestFixtureSetup()
    {
        _configuration = new Configuration();
        _configuration.Configure ();
        _configuration.AddAssembly(typeof(rapidstk_base.Domain.StockTransaction).Assembly);
        _sessionFactory = _configuration.BuildSessionFactory();
    }

    [SetUp]
    public void SetupContext()
    {
        var schema = new SchemaExport(_configuration);
        schema.Create(true, true);
        CreateInitialData();
    }

    [Test]
    public void CanAddTransaction ()
    {
        var newStkTransaction = new StockTransaction{ stkitm_StockItemToTransact=_stockitems[0], stkloc_StockLocation=_stocklocations[3], dcm_StockTransactionAmount=42.0M };
        IStockTransactionRepository repository = new StockTransactionRepository();
        repository.Add(newStkTransaction);

        using(ISession session = _sessionFactory.OpenSession())
        {
            var fromdb = session.Get<StockTransaction>(newStkTransaction.Id);
            Assert.IsNotNull(fromdb);
            Assert.AreNotSame(newStkTransaction, fromdb);
            Assert.AreEqual(newStkTransaction.stkitm_StockItemToTransact.str_StockItemName, fromdb.stkitm_StockItemToTransact.str_StockItemName);
            Assert.AreEqual(newStkTransaction.stkitm_StockItemToTransact.str_StockItemDescription, fromdb.stkitm_StockItemToTransact.str_StockItemDescription);
            Assert.AreEqual(newStkTransaction.stkloc_StockLocation.s_StockLocationName, fromdb.stkloc_StockLocation.s_StockLocationName);
            Assert.AreEqual(newStkTransaction.stkloc_StockLocation.s_StockLocationDescription, fromdb.stkloc_StockLocation.s_StockLocationDescription);
            Assert.AreEqual(newStkTransaction.dcm_StockTransactionAmount, fromdb.dcm_StockTransactionAmount);
        }

    }

    [Test]
    public void CanRemoveTransaction()
    {
        IStockTransactionRepository repository = new StockTransactionRepository();
        Guid id = _stockTransactions[0].Id;
        repository.Remove(_stockTransactions[0]);

        using(ISession session = _sessionFactory.OpenSession())
        {
            var fromdb = session.Get<StockLocation>(id);
            Assert.IsNull(fromdb);
        }
    }

    [Test]
    public void CanUpdateTransaction()
    {
        IStockTransactionRepository repository = new StockTransactionRepository();
        var stkTransaction = _stockTransactions[1];
        stkTransaction.dcm_StockTransactionAmount = 150.0M;
        repository.Update(stkTransaction);

        using(ISession session = _sessionFactory.OpenSession())
        {
            var fromdb = session.Get<StockTransaction>(stkTransaction.Id);
            Assert.AreEqual(stkTransaction.dcm_StockTransactionAmount, fromdb.dcm_StockTransactionAmount);
        }
    }

    [Test]
    public void CanGetAllTransactionForAProduct()
    {
        IStockTransactionRepository repository = new StockTransactionRepository();
        StockItem queryItem = _stockitems[4];
        var fromdb = repository.GetByStockItem(queryItem);

        Assert.AreEqual(4, fromdb.Count);
        Assert.IsTrue(IsInCollection(_stockTransactions[5], fromdb));
        Assert.IsTrue(IsInCollection(_stockTransactions[6], fromdb));
    }

    private bool IsInCollection(StockTransaction stkTrns, ICollection<StockTransaction> fromdb)
    {
        foreach(var item in fromdb)
            if(stkTrns.Id == item.Id)
                return true;
        return false;
    }

    [Test]
    public void CanGetAllTransactionsUpToADate()
    {
        IStockTransactionRepository repository = new StockTransactionRepository();
        StockItem queryItem = _stockitems[4];
        var fromdb = repository.GetByStockItem(queryItem, DateTime.Parse ("2011/12/02"));

        Assert.AreEqual(3, fromdb.Count);
        Assert.IsTrue(IsInCollection(_stockTransactions[5], fromdb));
        Assert.IsTrue(IsInCollection(_stockTransactions[6], fromdb));
        Assert.IsFalse(IsInCollection(_stockTransactions[7], fromdb));
    }

    [Test]
    public void CanGetAllTransactionsBetweenDates()
    {
        IStockTransactionRepository repository = new StockTransactionRepository();
        StockItem queryItem = _stockitems[4];
        var fromdb = repository.GetByStockItem(queryItem, DateTime.Parse ("2011/12/01"), DateTime.Parse ("2011/12/03"));

        Assert.AreEqual(3, fromdb.Count);
        Assert.IsFalse(IsInCollection(_stockTransactions[4], fromdb));
        Assert.IsTrue(IsInCollection(_stockTransactions[5], fromdb));
        Assert.IsTrue(IsInCollection(_stockTransactions[6], fromdb));
        Assert.IsTrue(IsInCollection(_stockTransactions[7], fromdb));
    }
}
}
使用NUnit.Framework;
使用NHibernate.Cfg;
使用NHibernate.Tool.hbm2ddl;
使用NHibernate;
使用制度;
使用System.Collections.Generic;
使用rapidstk_base.Domain;
使用rapidstk_base.Repositories;
使用rapidstk_基地;
命名空间rapidstk_基本测试
{
[测试夹具]
公共类StockTransactionRepositoryTest
{
私人ISessionFactory(sessionFactory);;
专用配置(u配置),;
私有只读静态StockItem[]\u stockitems=new[]
{
新的StockItem{str_StockItemName=“Orgone Accumulator”,str_StockItemDescription=“Social Integrator.”,
新的StockItem{str_StockItemName=“永久丁格尔”,str_StockItemDescription=“永久的幸福。”},
新StockItem{str_StockItemName=“Apple”,str_StockItemDescription=“Golden Delicious.”,
新StockItem{str_StockItemName=“Nepenthe”,str_StockItemDescription=“答案。”},
新StockItem{str_StockItemName=“Bobbins Gourd”,str_StockItemDescription=“跟随葫芦。”},
};
私有只读静态StockLocation[]\u stocklocations=new[]
{
new StockLocation(){s_StockLocationName=“Bay 1”,s_StockLocationDescription=“楼上咖啡机旁边。”},
new StockLocation(){s_StockLocationName=“Bay 2”,s_StockLocationDescription=“灌木丛下。”},
new StockLocation(){s_StockLocationName=“Bay Watch”,s_StockLocationDescription=“在海滩上弹跳。”},
new StockLocation(){s_StockLocationName=“我的口袋”,s_StockLocationDescription=“Lintville.”,
new StockLocation(){s_StockLocationName=“秘密巢穴”,s_StockLocationDescription=“可乐机旁边。”},
};
私有只读StockTransaction[]\u stockTransactions=new[]
{
新股票交易{stkitm_StockItemToTransact=_stockitems[0],stkloc_StockLocation=_stocklocations[0],dcm_StockTransactionMount=10.0M},
新股票交易{stkitm_StockItemToTransact=_stockitems[1],stkloc_StockLocation=_stocklocations[1],dcm_StockTransactionMount=-10.0M},
新股票交易{stkitm_StockItemToTransact=_stockitems[2],stkloc_StockLocation=_stocklocations[2],dcm_StockTransactionMount=1.0M},
新股票交易{stkitm_StockItemToTransact=_stockitems[3],stkloc_StockLocation=_stocklocations[3],dcm_StockTransactionMount=2.9M},
新股票交易{stkitm_StockItemToTransact=_stockitems[4],stkloc_StockLocation=_stocklocations[4],dcm_StockTransactionMount=155.0M,dt_StockTransactionDate=DateTime.Parse(“2011/11/30”),
新股票交易{stkitm_StockItemToTransact=_stockitems[4],stkloc_StockLocation=_stocklocations[4],dcm_StockTransactionMount=-50.0M,dt_StockTransactionDate=DateTime.Parse(“2011/12/01”),
新股票交易{stkitm_StockItemToTransact=_stockitems[4],stkloc_StockLocation=_stocklocations[4],dcm_StockTransactionMount=-50.0M,dt_StockTransactionDate=DateTime.Parse(“2011/12/02”),
新股票交易{stkitm_StockItemToTransact=_stockitems[4],stkloc_StockLocation=_stocklocations[4],dcm_StockTransactionMount=-50.0M,dt_StockTransactionDate=DateTime.Parse(“2011/12/03”),
};
私有void CreateInitialData()
{
使用(ISession session=\u sessionFactory.OpenSession())
{
使用(ITransaction transaction=session.BeginTransaction())
{
foreach(股票交易中的var tr)
{
session.Save(tr);
}
Commit();
}
}
}
[TestFixtureSetUp]
public void TestFixtureSetup()
{
_配置=新配置();
_Configure();
_AddAssembly(typeof(rapidstk_base.Domain.StockTransaction.Assembly);
_sessionFactory=_配置。BuildSessionFactory();
}
[设置]
公共void SetupContext()
{
var schema=新SchemaExport(_配置);
schema.Create(true,true);
CreateInitialData();
}
[测试]
公共无效交易()
{
var newStkTransaction=newstocktransaction{stkitm_StockItemToTransact=_stockitems[0],stkloc_StockLocation=_stocklocations[3],dcm_stocktransactionmount=42.0M};
IStockTransactionRepository=新的StockTransactionRepository();
repository.Add(newStkTransaction);
使用(ISession session=\u sessionFactory.OpenSession())
{
var fromdb=session.Get(newStkTransaction.Id);
Assert.IsNotNull(fromdb);
Assert.AreNotSame(newStkTransaction,fromdb);
Assert.AreEqual(newStkTransaction.stkitm_StockItemToTransact.str_StockItemName,fromdb.stkitm_StockItemToTransact.str_StockItemName);
Assert.AreEqual(新闻交易)
using System;
using System.Collections.Generic;
using rapidstk_base.Domain;
using NHibernate;
using NHibernate.Criterion;

namespace rapidstk_base.Repositories
{
public class StockTransactionRepository : IStockTransactionRepository
{
    public void Add(StockTransaction stkTransaction)
    {
        using(ISession session=NHibernateHelper.OpenSession())
            using(ITransaction transaction = session.BeginTransaction())
        {
            session.Save(stkTransaction);
            transaction.Commit();
        }
    }

    public void Remove(StockTransaction stkTransaction)
    {
        using(ISession session=NHibernateHelper.OpenSession())
            using(ITransaction transaction = session.BeginTransaction())
        {
            session.Delete(stkTransaction);
            transaction.Commit();
        }
    }

    public void Update(StockTransaction stkTransaction)
    {
        using(ISession session = NHibernateHelper.OpenSession())
            using(ITransaction transaction = session.BeginTransaction())
        {
            session.Update(stkTransaction);
            transaction.Commit();
        }
    }

    public ICollection<StockTransaction> GetByStockItem(StockItem item)
    {
        using(ISession session = NHibernateHelper.OpenSession())
        {
            var transactions = session.CreateCriteria(typeof(StockTransaction))
                                .Add(Restrictions.Eq("stkitm_StockItemToTransact", item))
                                .List<StockTransaction>();
            return transactions;
        }
    }

    public ICollection<StockTransaction> GetByStockItem(StockItem item, DateTime endDate)
    {
        using(ISession session = NHibernateHelper.OpenSession())
        {
            var transactions = session.CreateCriteria(typeof(StockTransaction))
                                .Add(Restrictions.Eq("stkitm_StockItemToTransact", item))
                                .Add(Restrictions.Le("dt_StockTransactionDate", endDate))
                                .List<StockTransaction>();
            return transactions;
        }
    }

    public ICollection<StockTransaction> GetByStockItem(StockItem item, DateTime startDate, DateTime endDate)
    {
        using(ISession session = NHibernateHelper.OpenSession())
        {
            var transactions = session.CreateCriteria(typeof(StockTransaction))
                                .Add(Restrictions.Eq("stkitm_StockItemToTransact", item))
                                .Add(Restrictions.Le("dt_StockTransactionDate", endDate))
                                .Add(Restrictions.Ge("dt_StockTransactionDate", startDate))
                                .List<StockTransaction>();
            return transactions;
        }
    }

    public StockTransactionRepository ()
    {

    }
}
}
[SetUp]
public void SetupContext()
{
    var schema = new SchemaExport(_configuration);
    schema.Create(true, true);
    InitializeData();    //create your StockItems, StockLocations, and StockTransactions
    CreateInitialData();
}