Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
44783480
Commit
44783480
authored
Apr 20, 2018
by
Mike Greiling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
document the spyOnDependency method
parent
c15584f2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
5 deletions
+43
-5
doc/development/testing_guide/frontend_testing.md
doc/development/testing_guide/frontend_testing.md
+43
-5
No files found.
doc/development/testing_guide/frontend_testing.md
View file @
44783480
...
...
@@ -126,13 +126,51 @@ it('tests a promise rejection', (done) => {
});
```
#### Stubbing
#### Stubbing
and Mocking
For unit tests, you should stub methods that are unrelated to the current unit you are testing.
If you need to use a prototype method, instantiate an instance of the class and call it there instead of mocking the instance completely.
Jasmine provides useful helpers
`spyOn`
,
`spyOnProperty`
,
`jasmine.createSpy`
,
and
`jasmine.createSpyObject`
to facilitate replacing methods with dummy
placeholders, and recalling when they are called and the arguments that are
passed to them. These tools should be used liberally, to test for expected
behavior, to mock responses, and to block unwanted side effects (such as a
method that would generate a network request or alter
`window.location`
). The
documentation for these methods can be found in the
[
jasmine introduction page
](
https://jasmine.github.io/2.0/introduction.html#section-Spies
)
.
For integration tests, you should stub methods that will effect the stability of the test if they
execute their original behaviour. i.e. Network requests.
Sometimes you may need to spy on a method that is directly imported by another
module. GitLab has a custom
`spyOnDependency`
method which utilizes
[
babel-plugin-rewire
](
https://github.com/speedskater/babel-plugin-rewire
)
to
achieve this. It can be used like so:
```
javascript
// my_module.js
import
{
visitUrl
}
from
'
~/lib/utils/url_utility
'
;
export
default
function
doSomething
()
{
visitUrl
(
'
/foo/bar
'
);
}
// my_module_spec.js
import
doSomething
from
'
~/my_module
'
;
describe
(
'
my_module
'
,
()
=>
{
it
(
'
does something
'
,
()
=>
{
const
visitUrl
=
spyOnDependency
(
doSomething
,
'
visitUrl
'
);
doSomething
();
expect
(
visitUrl
).
toHaveBeenCalledWith
(
'
/foo/bar
'
);
});
});
```
Unlike
`spyOn`
,
`spyOnDependency`
expects its first parameter to be the default
export of a module who's import you want to stub, rather than an object which
contains a method you wish to stub (if the module does not have a default
export, one is be generated by the babel plugin). The second parameter is the
name of the import you wish to change. The result of the function is a Spy
object which can be treated like any other jasmine spy object.
Further documentation on the babel rewire pluign API can be found on
[
its repository Readme doc
](
https://github.com/speedskater/babel-plugin-rewire#babel-plugin-rewire
)
.
### Vue.js unit tests
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment