Fork me on GitHub
rMock

Standalone mock library for brightscript


Introduction

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.

Getting Started #back to top

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.

Mock Object in action

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

                                

Documentation #back to top

Mock - #back to top

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()

                                
Properties of Mock Object
  • when( "methodName" ) - Takes in a parameter of type string. The parameter refers to the method that need to be mocked, so it overrides the existing method and returns an actions object.
  • expects( "methodName" ) - Takes in a parameter of type string. The parameter refers to the method that need to be mocked, so it overrides the existing method and returns an expection object.
  • verify() - Verifies all expectations set on the mock object. Returns a boolean value expressing the verification status
  • proxy() - Returns a mock copy of the object to be used as a dependency
  • restore() - Restores all mocked methods.

Expectation - #back to top

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 ] )
                                
Properties of Expectation Object
  • create( "methodName" ) - Takes in a string parameter which referes to a method name belonging within the mock object. It replaces the method's existing implementation with a mock function. Returns the expectation object.
  • once() - Expect the method once. Returns the expectation object.
  • twice() - Expect the method twice. Returns the expectation object.
  • thrice() - Expect the method thrice. Returns the expectation object.
  • exactly( numOfTimes ) - Expect the method to be called exactly to the set number of times. An integer parameter which sets the num of times the method is called. Returns the expectation object.
  • withArguments( values as Array ) - Expect the method to be called with the set argument values. It takes in an array as a parameter and the value in each array index corresponds to the parameter value in the mock method. Returns the expectation object.
  • never() - Expect the method to be never called. Returns the expectation object.
  • setMockObj() - Initializes the mock object

Actions - #back to top

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)
                                
Properties of Actions Object
  • create( "methodName" ) - Takes in a string parameter which referes to a method name belonging within the mock object. It replaces the method's existing implementation with a mock function. Returns the actions object.
  • returns( value as Dynamic ) - Sets a value to be returned when the mock function is called. The parameter could be any value.
  • setMockObj() - Initializes the mock object

Limitations #back to top

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
                                

Support #back to top

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.

Copyright and license #back to top

Code released under the Apache License 2.0

For more information about copyright and license check RMock License .