I am new to NHibernate and unit tests. I am attempting to follow the Summer of NHibernate videos. I am able to run unit tests successfully against my Member(the videos use customer object, I used member) object when there is no related data in any child tables. However when there are any related records in any of the child tables the unit tests fail with the following exception: (Don't jump to conclusions yet)
Setup.CanGetMemberByFirstName.TearDown
Failures
Exception
System.Data.SqlClient.SqlException: The DELETE statement conflicted with the
REFERENCE
constraint "FK_tblTransaction_tblMembers". The conflict occurred in database "TempPP2",
table "dbo.tblTransaction", column 'MemberID'.
The statement has been terminated.
Errors: System.Data.SqlClient.SqlErrorCollection
Class: 16
LineNumber: 1
Number: 547
Procedure:
Server: SOFTWARE2\SQL2008E
State: 0
ErrorCode: -2146232060
TargetSite: Void PerformDbOperation(NDbUnit.Core.DbOperationFlag)
HelpLink: null
at NDbUnit.Core.NDbUnitTest.PerformDbOperation(DbOperationFlag dbOperationFlag)
at Microdesk.Utility.UnitTest.DatabaseUnitTestBase.LoadDatabase(String connectionString, String schemaFilePathName, String datasetFilePathName, DatabaseClientType clientType)
at Microdesk.Utility.UnitTest.DatabaseUnitTestBase.LoadTestDatabase()
at Microdesk.Utility.UnitTest.DatabaseUnitTestBase.DatabaseSetUp()
at PPDataTest.UnitTest.Setup()
Now the exception is pretty obvious. It appears that a member is being deleted that has a child record in the tblTransaction table. However, the method being called (CanGetMemberByFirstName) is only running a select statement.
<Test()> _
Public Sub CanGetMemberByFirstName()
Const MEMBER_FIRSTNAME As String = "ERIC"
Dim members As IList(Of Member) = _provider.GetMembersByFirstName(MEMBER_FIRSTNAME)
For Each m As Member In members
Assert.AreEqual(MEMBER_FIRSTNAME, m.FirstName)
Next
End Sub
The teardown method is probably where the delete statement is occurring, but I don't know how to resolve this issue. To summarize, all tests fail when there are related records in the tblTransactions table even if there are no records for the member object I am querying. It also makes no difference which CRUD operation is being performed, the TearDown method throws the exception.
Has anyone experienced the same of similar issues when following the videos? Is there a better way to perform unit testing against a database/NHibernate?
EDIT: From what I understand, the purpose of the TearDown method is to Undo database operations that were performed during the Unit Test.
Thanks, Eric
More Info: The test fixture setup methods.
<TestFixtureSetUp()> _
Public Sub TestFixtureSetup()
_provider = New PPData.DataProvider
DatabaseFixtureSetUp()
End Sub
<TestFixtureTearDown()> _
Public Sub TestFixtureTearDown()
DatabaseFixtureTearDown()
End Sub
<SetUp()> _
Public Sub Setup()
DatabaseSetUp()
End Sub
<TearDown()> _
Public Sub TearDown()
DatabaseTearDown()
End Sub
Private Sub GetMyTestDataXMLFile()
SaveTestDatabase()
End Sub