   LotusScript-

   LotusScript-    .
 LSUnit  ,       .
 FakeObjects  -  Notes-,              Lotus Notes.
        (unit tests).       .       ,      ,  Notes-,      .

 

    ,     TestCase:

	Public Class TestSomething As TestCase

		Public Sub test1()

		End Sub

	End Class

,    ,       :

	Private Const LIBRARY_NAME = "TestSomething"

	Public Class TestSomethingSuite As TestSuite

		Public Sub new()
			Call AddTestCase(LIBRARY_NAME, "TestSomething", "test1")
		End Sub

	End Class

    ,      :

	Use "TestSomething"

	Sub Initialize
		Dim runner As New TestRunner()
		Dim testSuite As TestSuite
		Set testSuite = New TestSomethingSuite()
		Call runner.run(testSuite)
	End Sub

        :

		Dim runner As New TestRunner()
		Dim testSuite As New TestSuite()
		Dim suite As TestSuite
		
		Set suite = New TestSomethingSuite()
		Call testSuite.addTestSuite(suite)

		Set suite = New AnotherTestSuite()
		Call testSuite.addTestSuite(suite)

		Call runner.run(testSuite)
	
     : , , , .
1.   :  (   ),  (  ()    )   (   TestCase.setUp()     ).
2.   .
3.          (assertTrue, assertEquals etc.).
4. ,    (  TestCase.tearDown())  (  ).

 .

	Public Class DocumentProcessor
	
		Public Sub processDocument(document As Variant)
			On Error Goto errorHandler
			'... do something
			If document.getItemValue("Count")(0) = 0 Then Error APPLICATION_ERROR_CODE, "Application error"
			'... do something
			Call document.replaceItemValue("Status", "OK")
			Exit Sub
	errorHandler:
			Call document.replaceItemValue("Status", "Error")
			Exit Sub
		End Sub
	
	End Class
	
	
	Public Class TestDocumentProcessor As TestCase
	
		Private document As Variant
		
		Private Sub setUp()
			Set document = New FakeDocument(Nothing)
		End Sub
	
		Public Sub testSuccessCall()
			Dim processor As New DocumentProcessor()
			Call document.replaceItemValue("Count", 1)
			Call processor.processDocument(document)
			Call assertEquals("status", "OK", document.getItemValue("Status"))
		End Sub

	End Class
	
  :
1. .   setUp()    .        (Call document.replaceItemValue("Count", 1)),       ( DocumentProcessor).
2. .   processDocument()  .
3. .     assertEquals().   - ,    ,    .     -  .
4. .  ,   tearDown()    .

     (       ),    :

		Public Sub testRaisedException()
			Dim sut As New SystemUnderTest()
			On Error Goto ok
			Call sut.raiseException()
			On Error Goto 0
			Call fail("exception expected")
			Exit Sub
	ok:
			Exit Sub
		End Sub
	


 LSUnit

      .
 : TestCase, TestSuite, TestResult, TestRunner.

 TestCase      .        :
assertTrue() -    ;
assertFalse() -    ;
assertNothing()  assertNotNothing()    ;
assertEquals() -     ;
assertArrayContains() -      ;
assertArrayNotContains() -     .
       ,    ,    .
       , ,  .
    setUp()  tearDown(),      , .

 TestSuite ( )     ( )    .      .
 addTestCase(libraryName As String, className As String, methodName As String)        methodName  className,    libraryName.
 addTestSuite(suite As TestSuite)      .           ,           .
 run(result As TestResult)    ,   ,      .       result.
  runTestCase(),    run(),   Execute  LotusScript        .

 TestResult        .
      (. TestSuite.runTestCase())  ,    TestResult.addError().          ,      (.   'msg = eh_getStackTrace("LSUnit", Typename(Me)) : 'Call eh_clearStackTrace()).
  assertXXX  ,   TestCase.fail(),         TestResult.addFailure().

 TestRunner             .       (Messagebox),       ,          .      ,      .       ,    .


 FakeObjects

   -,     Notes-  .
,     Notes- ,      80 ,     getItemValue/replaceItemValue.
      .      (   ) ,     .       -    .           ,  (, , ).
  -     FakeObjectsTest.lss,    .

   ,     FakeObjects.

1.  FakeDatabase.Search().
    Variant,    NotesDatabase.Search()  .               :

	Public Class MyCoolClass

		Private database As Variant
		
		Private Function findDocuments() As Variant
			Const query = {Form = "Memo"}
			If database Is NotesDatabase Then
				Dim db As NotesDatabase
				Set db = database
				Set findDocuments = db.Search(query, Nothing, 0)
			Else
				Set findDocuments = database.Search(query, Nothing, 0)
			End If
		End Function
		
	End Class

 :      :
	
	Public Class MyCoolClass
	
		Private database As Variant
	
		Private Function getQuery() As String
			getQuery = {Form = "Memo"}
		End Function

		Private Function findDocuments() As Variant
			Dim db As NotesDatabase
			Set db = database
			Set findDocuments = db.Search(getQuery(), Nothing, 0)
		End Function
		
	End Class

    ,     findDocuments():

	Public Class MyCoolClassTesting As MyCoolClass

		Private Function findDocuments() As Variant
			Set findDocuments = database.Search(getQuery(), Nothing, 0)
		End Function

	End Class

    :

		Dim sut As MyCoolClass
		Set sut = New MyCoolClassTesting()
		'... do something


2.  Readers/Authors .
 ,    Readers/Authors        NotesItem     .    ,   Notes-,    ?  .         :

		Public Function createReadersField(database As Variant, fieldName As String, values As Variant) As Variant
			Set createReadersField = New NotesItem(database, fieldName, values, READERS)
		End Function

       :

		Public Function createReadersField(database As Variant, fieldName As String, values As Variant) As Variant
			Set createReadersField = New FakeSpecialItem(database, fieldName, values, READERS)
		End Function

,       .    , -,       (  -     ),  -,     ,     .  -    .


3.    -. ,    ,   NotesDocument.Save(False, False)  False.   save -   True.      FakeDocument:

	Public Class FakeDocumentWhichCannotBeSaved As FakeDocument
	
		Public Sub New(database As Variant), FakeDocument(database)
		End Sub
	
		Public Function save(force As Boolean, makeResponse As Boolean) As Boolean
			save = False
		End Function
	
	End Class

       :

		Public Sub testDocumentCannotBeSaved()
			Dim document As Variant
			Dim sut As SUT
			...
			Set document = New FakeDocumentWhichCannotBeSaved(Nothing)
			Call sut.doSomething(document)
			Call assertFalse("status", sut.isSuccess())
		End Sub



 FakeObjects   .           ,     ,      .


 LSUnit  FakeObjects   Public Domain (" ").        .   , ,   .          .


 , 2009 
mailto:ls.bincode@gmail.com
