Standalone mock library for brightscript
This documentation gives a brief overview of how to use the rMock library to write tests. You can find more specific details of each object within library described below.
This mock library works with Roku's Unit Test Framework . You can simply call on the Mock() methods within the testcase definition to make verifications.
You need to include the rMock.brs file into your project source folder. You can place this file in any folder as you wish as along as it's within the source root folder. This would enable the mock methods to be available within the test scope. For scenegraph components, ensure that the rMock.brs is referenced within the scene component.
Here is a sample mock object in action. In the example below, the subject under test is the ScaleService() . The scale service does the work of simply noting down a measure and displaying the measurement in a given format. The format value is based on another object UnitConversionService() . This object contains an implementation two methods that convert a measure from pound to kilograms and vice versa.
function TestCase_DisplayMeasurement() ' Set up mockUnitConvertor = Mock( UnitConversionService() ) mockUnitConvertor.when("lbToKg").returns(1.81437) mockUnitConvertor.expects("lbTokg").once().withArguments( [ 4.0 ] ) ' Exercise scale = ScaleService( mockUnitConvertor.proxy() ) scale.Measure( "lb", 4 ) measurement = scale.DisplayMeasurement( "kg" ) ' Verify mockStatus = mockUnitConvertor.verify() isEquals = ( measurement = "1.81437kg" ) return m.assertTrue( ( mockStatus AND isEquals ) ) end function
The Mock() method returns a mock object. It is a special object that provides a few methods in which mockist style testing can be performed. The mock object takes in a parameter. The parameter represents the object that is intended to be mocked.
mockUnitConvertor = Mock( UnitConversionService() ) ' use the mockUnitConvertor in any Subject under test mockUnitConvertor.verify()
The Expectation() method returns an expectation object. It provides methods which can be used to set expectations on the object's method being mocked.
mockUnitConvertor.expects("lbTokg").once().withArguments( [ 4.0 ] )
The Actions() method returns an actions object. It provides methods which can be used to set actions on the object's method being mocked.
mockUnitConvertor.when("lbToKg").returns(1.81437)
One major limitation is that there can only be one method mocked within an object at a time. This error exists as a result of a function not able to get a reference of itself when executing. To work around this limitation, we suggest that you use the MockObj.restore() when needed. This would enable the ability to set expectations and verify each method independantly.
function TestCase_DisplayMeasurement() ' Set up mockUnitConvertor = Mock( UnitConversionService() ) mockUnitConvertor.when("lbToKg").returns(1.81437) mockUnitConvertor.expects("lbTokg").once().withArguments( [ 4.0 ] ) ' Exercise scale = ScaleService( mockUnitConvertor.proxy() ) scale.Measure( "lb", 4 ) measurement = scale.DisplayMeasurement( "kg" ) ' Verify mockStatus = mockUnitConvertor.verify() isEquals = ( measurement = "1.81437kg" ) ' Restore mockUnitConvertor.restore() mockUnitConvertor.when("kgTolb").returns(4) mockUnitConvertor.expects("kgTolb").once().withArguments( [ 1.81437 ] ) scale.DisplayMeasurement( "lb" ) status = mockUnitConvertor.verify() return m.assertTrue( ( mockStatus AND isEquals ) ) end function
This is an OpenSource project. You can find the code repo hosted on Github. If you find any issues or have ideas on how to improve this library, please feel free to elaborate more on issues section within Github. The repo is actively monitored and you can expect a quick response from the admins.
We welcome your contribution to the project. Please go over the contribution guidelines to start contributing.
Code released under the Apache License 2.0
For more information about copyright and license check RMock License .