Skip to content

Commit

Permalink
chore(docs): update mockFunction API documentation (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkipke authored Apr 1, 2021
1 parent 15efd96 commit 1f0ed24
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
54 changes: 49 additions & 5 deletions docs/api/reference/mocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,37 @@ Dynamically overwrite a function's implementation, and spy on the mocked impleme
**funcName** `string` \
The name of the function to overwrite.
------------
**impl** `function` \
The function implementation
**impl** `function` _optional_ \
The function implementation. If omitted, a default implementation will be created that accepts any number of arguments (up to 10) and returns `invalid`.
------------

### Return value
[Mock function](#mock-function-api)

### Usage
Original function definition:
```brightscript
mock = _brs_.mockFunction("FunctionName", sub()
print "foobar"
end sub)
function myFunc(requiredArg as string)
return "myFunc: " + requiredArg
end function
```

Test case:
```brightscript
' original implementation
print myFunc("foo") ' => "myFunc: foo"
' mocked with a custom implementation
mock = _brs_.mockFunction("myFunc", function()
return "foobar"
end function)
print myFunc() ' => "foobar"
print myFunc("foo") ' => runtime error because the signatures don't match
' implicitly mocked -- can be called with or without args
mock = _brs_.mockFunction("myFunc")
print myFunc() ' => invalid
print myFunc(123, {}, "foo") ' => invalid
```
<br/>

Expand Down Expand Up @@ -295,6 +314,31 @@ print mock.getMockName() ' => "fooBar"

------------

## mock.mockReturnValue(value)
Sets a new return value for a mocked function. This overwrites any existing mock implementation.

### Parameters
**value** `dynamic` \
The value that this mocked function should return.
------------

### Return value
None.

### Usage
```brightscript
mock = _brs_.mockFunction("fooBar", function()
return "hello, world"
end function)
print fooBar() ' => "hello, world"
mock.mockReturnValue(123)
print fooBar() ' => 123
```
<br/>

------------

## mock.results
An array containing the return value for each call to this function.

Expand Down
4 changes: 2 additions & 2 deletions docs/guides/testing-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ If `FooComponent` uses the `foo` function as a callback:
<!-- Field with a callback we want to test -->
<field id="myField" type="string" onChange="foo" />

<!-- Field with a callback we want to test -->
<field id="sideEffectField" type="string">
<!-- Field that gets modified as a side-effect of the `foo` function -->
<field id="sideEffectField" type="string" value="bar">
</interface>
</component>
```
Expand Down

0 comments on commit 1f0ed24

Please sign in to comment.