unit-testing - Fluent NHibernate PersistenceSpecification 组件和引用测试

标签 unit-testing nhibernate fluent-nhibernate testing components

我有两个问题。

1 。 FNH 不测试我的组件正确性,我不知道为什么。

System.ApplicationException: Expected 'DomainModel.Model.Publisher' but got 'DomainModel.Model.Publisher' for Property 'Publisher'.

[TestMethod]
public void CanCorrectlyMapBook()
{
    new PersistenceSpecification<Book>(_session)
        .CheckProperty(p => p.Name, "My Book")
        .CheckProperty(p=> p.Id, 1)
        .CheckProperty(p=>p.IncludesCDDVD, true)
        .CheckProperty(p=>p.Isbn, "rder93q43949éwr")
        .CheckProperty(p=>p.IsLoaned, false)
        .CheckProperty(p=>p.Publisher, new Publisher(){PublisherHomepage = "www.google.de", PublisherName = "google"})
        .VerifyTheMappings();
}

2。 FNH 没有正确测试我的引用。

System.ApplicationException: Expected 'DomainModel.Model.Employee' but got 'EmployeeProxyd6f94daa37c74be8b5ccccf40c5c23fa' for Property 'LoanedBy'.

[TestMethod]
public void CanCorrectlyMapBook()
{
    new PersistenceSpecification<Book>(_session)
        .CheckProperty(p => p.Name, "My Book")
        .CheckProperty(p=> p.Id, 1)
        .CheckProperty(p=>p.IncludesCDDVD, true)
        .CheckProperty(p=>p.Isbn, "rder93q43949éwr")
        .CheckProperty(p=>p.IsLoaned, false)
        .CheckReference(p=>p.LoanedBy, new Employee(){EMail = "",FirstName = "Alex", LastName = "Mueller"})
        .VerifyTheMappings();
}

但是当我“手动”测试这个时,一切正常。

 ISession mysession = Helper.CreateSessionFactory(false, false).OpenSession();
            Book myBook = new Book()
                              {
                                  Author = "Hesse",
                                  IncludesCDDVD = true,
                                  DateOfIssue = DateTime.Now,
                                  Isbn = "erwe0ri",
                                  IsLoaned = true,
                                  Name = "My Book new",
                                  Publisher = new Publisher() { PublisherHomepage = "www.google.de", PublisherName = "google" },
                                  Release = new Release() { ReleaseDate = DateTime.Now, ReleaseNumber = 1 },
                                  LoanedBy = new Employee() { EMail = "", FirstName = "Alex", LastName = "Mueller" }
                              };

            mysession.Save(myBook);
            mysession.Close();
            mysession.Dispose();

我已经通过在数据库中查找来验证这一点......

PersistenceSpecification 测试针对内存数据库 sqllite 运行,而我的手动“测试”针对 Sql Server 2008 运行。

你们中有人使用过 FNH 并正确测试过引用和组件吗?

最佳答案

我认为您需要在相关实体上实现 object.Equals() 方法,或者实现 IEqualityComparer 并在构造 PersistenceSpecification 时注入(inject)它。

例如:

public class A
{
    private int Id { get; set; }
    public virtual B B_Member { get; set; }

    public class Map : ClassMap<A>
    {
        public Map()
        {
            Id(x => x.Id);
            References(x => x.B_Member);
        }
    }
}

public class B
{
    private int Id { get; set; }
    public virtual string BString { get; set; }

    public class Map : ClassMap<B>
    {
        public Map()
        {
            Id(x => x.Id);
            Map(x => x.BString);
        }
    }

    /// remove this method to have the verification fail
    public override bool Equals(object obj)
    {
        var lhs = obj as B;
        if (lhs == null) return false;
        return BString == lhs.BString;
    }
}

    [Test]
    public void Verify()
    {
        var fcfg = Fluently.Configure()
            .Database(SQLiteConfiguration.Standard.UsingFile("testdb.sqldb"))
            .Mappings(mc =>
            {
                mc.FluentMappings.Add(typeof (A.Map));
                mc.FluentMappings.Add(typeof (B.Map));
            })
            .ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(true, true, false));

        var sess = fcfg.BuildSessionFactory().OpenSession();

        new PersistenceSpecification<A>(sess)
            .CheckReference(x => x.B_Member, new B() {BString = "hi"})
            .VerifyTheMappings();

        Assert.Throws<ApplicationException>(
            () => new PersistenceSpecification<A>(sess, new AlwaysFalseEqualityComparer())
                    .CheckReference(x => x.B_Member, new B() {BString = "az"})
                    .VerifyTheMappings());
    }

另请注意,每个属性比较的相关 FNH 代码是(反射器的赞美):

    internal virtual void CheckValue(object target)
{
    bool areEqual;
    object actual = this.property.GetValue(target, null);
    if (this.entityEqualityComparer != null)
    {
        areEqual = this.entityEqualityComparer.Equals(this.propertyValue, actual);
    }
    else
    {
        areEqual = this.propertyValue.Equals(actual);
    }
    if (!areEqual)
    {
        throw new ApplicationException(string.Format("Expected '{0}' but got '{1}' for Property '{2}'", this.propertyValue, actual, this.property.Name));
    }
}

当然,该异常似乎与您遇到的异常相匹配。

关于unit-testing - Fluent NHibernate PersistenceSpecification 组件和引用测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1776120/

相关文章:

c# - Microsoft.VisualStudio.TestPlatform.TestFramework 和 Microsoft.VisualStudio.QualityTools.UnitTestFramework 之间的区别

nhibernate - SQLite 与 NHibernate 结合是否支持参照完整性/外键?

nhibernate - 如何获取带有异常参数值的 SQL

nhibernate - 您是否将NHibernate用于具有遗留数据库的项目,而遗留数据库是部分无法控制的?

nhibernate - 如何使用 Fluent-nhibernate 将集合计数映射到实体

c# - NHibernate w/Fluent - 没有 : [class] 的持久性

java - Mockito When() 不起作用

node.js - 为什么多次调用 jest mockResolvedValueOnce 返回相同的值?

python - Travis 无法在 Python 3 变体中导入我的测试模块

c# - 将 NHibernate 生成的所有 sql 导出到一个文本文件