User bio
404 bio not found
Member since Nov 8, 2018
Replies:

Here we are using the mocking framework that we developed (GitHub - GendronAC/InterSystems-UnitTest-Mocking: This project contains a mocking framework to use with InterSystems' Products written in ObjectScript

Have a look at the https://github.com/GendronAC/InterSystems-UnitTest-Mocking/blob/master/S... class. Instead of calling ..SendRequestAsync we do ..ensHost.SendRequestAsync(...) Doing so enables us to create Expectations (..Expect(..ensHost.SendRequestAsync(....

Here a code sample : 

Class Sample.Src.CExampleService Extends Ens.BusinessService
{

/// The type of adapter used to communicate with external systems
Parameter ADAPTER = "Ens.InboundAdapter";

Property TargetConfigName As %String(MAXLEN = 1000);

Parameter SETTINGS = "TargetConfigName:Basic:selector?multiSelect=0&context={Ens.ContextSearch/ProductionItems?targets=1&productionName=@productionId}";

// -- Injected dependencies for unit tests

Property ensService As Ens.BusinessService [ Private ];

/// initialize Business Host object
Method %OnNew(
	pConfigName As %String,
	ensService As Ens.BusinessService = {$This}) As %Status
{
   set ..ensService = ensService
   return ##super(pConfigName)
}

/// Override this method to process incoming data. Do not call SendRequestSync/Async() from outside this method (e.g. in a SOAP Service or a CSP page).
Method OnProcessInput(
	pInput As %RegisteredObject,
	Output pOutput As %RegisteredObject,
	ByRef pHint As %String) As %Status
{
   set output = ##class(Ens.StringContainer).%New("Blabla")

   return ..ensService.SendRequestAsync(..TargetConfigName, output)
}

}
Import Sample.Src

Class Sample.Test.CTestExampleService Extends Tests.Fw.CUnitTestBase
{

Property exampleService As CExampleService [ Private ];

Property ensService As Ens.BusinessService [ Private ];

ClassMethod RunTests()
{
   do ##super()
}

Method OnBeforeOneTest(testName As %String) As %Status
{
   set ..ensService = ..CreateMock()

   set ..exampleService = ##class(CExampleService).%New("Unit test", ..ensService)
   set ..exampleService.TargetConfigName = "Some test target"

   return ##super(testName)
}

// -- OnProcessInput tests --

Method TestOnProcessInput()
{
   do ..Expect(..ensService.SendRequestAsync("Some test target",
                                             ..NotNullObject(##class(Ens.StringContainer).%ClassName(1)))
               ).AndReturn($$$OK)

   do ..ReplayAllMocks()

   do $$$AssertStatusOK(..exampleService.OnProcessInput())

   do ..VerifyAllMocks()
}

Method TestOnProcessInputFailure()
{
   do ..Expect(..ensService.SendRequestAsync("Some test target",
                                             ..NotNullObject(##class(Ens.StringContainer).%ClassName(1)))
               ).AndReturn($$$ERROR($$$GeneralError, "Some error"))

   do ..ReplayAllMocks()

   do $$$AssertStatusNotOK(..exampleService.OnProcessInput())

   do ..VerifyAllMocks()
}

}
Certifications & Credly badges:
André-Claude has no Certifications & Credly badges yet.
Global Masters badges:
André-Claude has no Global Masters badges yet.
Followers:
André-Claude has no followers yet.
Following:
André-Claude has not followed anybody yet.