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
fda4d554
Commit
fda4d554
authored
Jun 20, 2018
by
Lin Jen-Shin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add docs around expect_next_instance_of
parent
562f357b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
0 deletions
+48
-0
doc/development/gotchas.md
doc/development/gotchas.md
+48
-0
No files found.
doc/development/gotchas.md
View file @
fda4d554
...
...
@@ -92,6 +92,54 @@ describe API::Labels do
end
```
## Avoid using `allow_any_instance_of` in RSpec
### Why
*
Because it is not isolated therefore it might be broken at times.
*
Because it doesn't work whenever the method we want to stub was defined
in a prepended module, which is very likely the case in EE. We could see
error like this:
1.1) Failure/Error: allow_any_instance_of(ApplicationSetting).to receive_messages(messages)
Using `any_instance` to stub a method (elasticsearch_indexing) that has been defined on a prepended module (EE::ApplicationSetting) is not supported.
### Alternative: `expect_next_instance_of`
Instead of writing:
```
ruby
# Don't do this:
allow_any_instance_of
(
Project
).
to
receive
(
:add_import_job
)
```
We could write:
```
ruby
# Do this:
expect_next_instance_of
(
Project
)
do
|
project
|
expect
(
project
).
to
receive
(
:add_import_job
)
end
```
If we also want to expect the instance was initialized with some particular
arguments, we could also pass it to
`expect_next_instance_of`
like:
```
ruby
# Do this:
expect_next_instance_of
(
MergeRequests
::
RefreshService
,
project
,
user
)
do
|
refresh_service
|
expect
(
refresh_service
).
to
receive
(
:execute
).
with
(
oldrev
,
newrev
,
ref
)
end
```
This would expect the following:
```
ruby
# Above expects:
refresh_service
=
MergeRequests
::
RefreshService
.
new
(
project
,
user
)
refresh_service
.
execute
(
oldrev
,
newrev
,
ref
)
```
## Do not `rescue Exception`
See
[
"Why is it bad style to `rescue Exception => e` in Ruby?"
][
Exception
]
.
...
...
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