lundi 8 août 2016

NUnit test how to prepare/modify function

Probably simple question but i am beginner in this area. Have NUnit project and would like to test similar functions like this example below. As you see i pass some id to it and then looping through, when data found i delete records.

Now with regards to test i would like instead of delete i would like to get those id in test project, so i pass id and get all of colelcted id's to see if they are as expected without deletion.

What i got on mind is to extend all functions with additional variables to store id's then add additional paraemeter to function like isTest as Boolean and in places where are deletes and for all Delete methods make if Test then not delete but add ids to variables, but i think its very bad idea.

How to do that?

 Public Sub DelEverythingAssociatedWithSection(secId As Integer)
        Using con As New SqlConnection(strcon)
            con.Open()
            Using transaction = con.BeginTransaction
                Try
                    Dim dtHtmlSection_KatSubkatDAL As New DataTable
                    dtHtmlSection_KatSubkatDAL = CType(New HtmlSection_KatSubkatDAL().GetAllBySecId(secId), DataTable)
                    If dtHtmlSection_KatSubkatDAL IsNot Nothing Then
                        For Each sec_katsubkat As DataRow In dtHtmlSection_KatSubkatDAL.Rows
                            Dim dtSubSec_SecKatSubKat_SubSubKat As New DataTable
                            dtSubSec_SecKatSubKat_SubSubKat = CType(New DALSubSec_SecKatSubKat_SubSubKat().GetAllBySec_KatSub(CInt(sec_katsubkat(0))), DataTable)
                            If dtSubSec_SecKatSubKat_SubSubKat IsNot Nothing Then
                                For Each subsec As DataRow In dtSubSec_SecKatSubKat_SubSubKat.Rows
                                    Dim dtHtmlSentence_SubSec_SecKatSubKat_SubSubKat As New DataTable
                                    dtHtmlSentence_SubSec_SecKatSubKat_SubSubKat = CType(New HtmlSentence_SubSec_SecKatSubKat_SubSubKatDAL().GetAllBySubSec_SecKatSubKat_SubSubKat(CInt(subsec(0))), DataTable)
                                    If dtHtmlSentence_SubSec_SecKatSubKat_SubSubKat IsNot Nothing Then
                                        For Each sent As DataRow In dtHtmlSentence_SubSec_SecKatSubKat_SubSubKat.Rows
                                            Dim dtArtikel_Beschreibung As New DataTable
                                            dtArtikel_Beschreibung = CType(New Artikel_BeschreibungDAL().GetAllBySentence(CInt(sent(0))), DataTable)
                                            If dtArtikel_Beschreibung IsNot Nothing Then
                                                For Each artBesch As DataRow In dtArtikel_Beschreibung.Rows
                                                    Call New Artikel_BeschreibungDAL().Delete(CInt(artBesch(0)), transaction)
                                                Next
                                            End If
                                            Call New HtmlSentence_SubSec_SecKatSubKat_SubSubKatDAL().Delete(CInt(sent(0)), transaction)
                                        Next
                                    End If
                                    Call New DALSubSec_SecKatSubKat_SubSubKat().Delete(CInt(subsec(0)), transaction)
                                Next
                            End If
                            Call New HtmlSection_KatSubkatDAL().Delete(CInt(sec_katsubkat(0)), transaction)
                        Next
                    End If
                    Call New DALSection().Delete(secId, transaction)
                    'If we made it this far without an exception, then commit.
                    transaction.Commit()
                Catch ex As Exception
                    transaction.Rollback()
                    Throw 'Rethrow exception.
                End Try
            End Using
        End Using
    End Sub

Aucun commentaire:

Enregistrer un commentaire