开发者

How can NHibernate make Unit Test Easy?

NHibernate uses XML files to map the classes to开发者_Go百科 the tables in the database. How can unit testing XML files be easier than program code (C#, JAVA, etc.)? Does NHibernate assume that the mapping XML files are automatically perfect and there is no need to test them?


Fluent NHibernate uses classes instead of XML for mapping and I believe has some support for testing.


Unit test your DAO, which encapsulates your NHibernate queries, to verify that your NHibernate configuration/mappings are correct.


Over the last several years I've taken 3 different approaches:

1 - Don't do any testing. Find out when you deploy to a test server whether the mappings are correct. This is not ideal.

2 - I wrote some code that, using reflection, found all my mapped POCOs and would automatically instantiate them (it was kind of recursive so it'd even explore the foreign keys) then get them back from the DB to see if all the properties were persisting correctly.

3 - Wrote integration tests around my web services. I allowed these tests to exercise the web services code which went all the way to the DB. This turned up any mapping problems.

EDIT - The one other thing I'd mention is that NHibnerate does have a nice feature where you can use an in-memory database. This can be useful for tests where you don't want to mock away NHibnerate but you don't want your tests to depend on a (real) database server.


i am not sure if this can solve your problem or not ... basically if you using nhibernate then you better use fluent nhibernate . which actually builds up proxy classes using POCO instead of hbm.xml mappings which makes your life lot easier for refactoring ..

on your question of unit testing i am myself looking for some clues for unit test scenarios .. say for example if u have a student belongs to department then you actually need to write test cases. You wont be needing any coding for manual testing from deployment. that would solve the problem as of now for manual testing it would be lot more easier cause writing test fixtures makes u test your own code without manual check

here is an example

[TestFixtureSetUp]
    public void CanMapCorrectlyStudent()
    {
        using (ISession session = PersistanceManager.OpenSession())
        {
            new PersistenceSpecification<Student>(session)  
                .CheckProperty(c=>c.Studentid,5)
                .CheckProperty(c => c.Name, "John")
                .CheckProperty(c=>c.Age , 24)                                        
                .CheckReference(c=>c.Department,new Department { Dept_id = 1 , Dept_name="MCA" })
                .VerifyTheMappings();
        }
    }

you can find the corresponding example in nhibernate wiki as wel.. install testdriven.net for visual studio , right click on this function body mention above and click run test . it would automatically run the test for you freeing you of all the hassles for manual coding and rechecking the tests

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜