2016-06-02 11:05:42 +05:30
# Gotchas
The purpose of this guide is to document potential "gotchas" that contributors
might encounter or should avoid during development of GitLab CE and EE.
2017-08-17 22:00:37 +05:30
## Do not assert against the absolute value of a sequence-generated attribute
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
Consider the following factory:
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
```ruby
2018-03-17 18:26:18 +05:30
FactoryBot.define do
2017-08-17 22:00:37 +05:30
factory :label do
sequence(:title) { |n| "label#{n}" }
end
end
```
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
Consider the following API spec:
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
```ruby
require 'rails_helper'
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
describe API::Labels do
it 'creates a first label' do
create(:label)
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
get api("/projects/#{project.id}/labels", user)
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
expect(response).to have_http_status(200)
expect(json_response.first['name']).to eq('label1')
end
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
it 'creates a second label' do
create(:label)
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
get api("/projects/#{project.id}/labels", user)
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
expect(response).to have_http_status(200)
expect(json_response.first['name']).to eq('label1')
end
end
2016-06-02 11:05:42 +05:30
```
2017-08-17 22:00:37 +05:30
When run, this spec doesn't do what we might expect:
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
```sh
1) API::API reproduce sequence issue creates a second label
Failure/Error: expect(json_response.first['name']).to eq('label1')
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
expected: "label1"
got: "label2"
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
(compared using ==)
2016-06-02 11:05:42 +05:30
```
2018-03-17 18:26:18 +05:30
That's because FactoryBot sequences are not reseted for each example.
2017-08-17 22:00:37 +05:30
Please remember that sequence-generated values exist only to avoid having to
explicitly set attributes that have a uniqueness constraint when using a factory.
2016-06-02 11:05:42 +05:30
### Solution
2017-08-17 22:00:37 +05:30
If you assert against a sequence-generated attribute's value, you should set it
explicitly. Also, the value you set shouldn't match the sequence pattern.
For instance, using our `:label` factory, writing `create(:label, title: 'foo')`
is ok, but `create(:label, title: 'label1')` is not.
Following is the fixed API spec:
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
```ruby
require 'rails_helper'
describe API::Labels do
it 'creates a first label' do
create(:label, title: 'foo')
get api("/projects/#{project.id}/labels", user)
expect(response).to have_http_status(200)
expect(json_response.first['name']).to eq('foo')
end
it 'creates a second label' do
create(:label, title: 'bar')
get api("/projects/#{project.id}/labels", user)
expect(response).to have_http_status(200)
expect(json_response.first['name']).to eq('bar')
end
end
2016-06-02 11:05:42 +05:30
```
2019-02-15 15:39:39 +05:30
## Avoid using `expect_any_instance_of` or `allow_any_instance_of` in RSpec
2018-11-08 19:23:39 +05:30
### Why
2019-03-02 22:35:43 +05:30
- 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
2018-11-08 19:23:39 +05:30
in a prepended module, which is very likely the case in EE. We could see
error like this:
2018-11-20 20:47:30 +05:30
```
2019-02-15 15:39:39 +05:30
1.1) Failure/Error: expect_any_instance_of(ApplicationSetting).to receive_messages(messages)
2018-11-20 20:47:30 +05:30
Using `any_instance` to stub a method (elasticsearch_indexing) that has been defined on a prepended module (EE::ApplicationSetting) is not supported.
```
2018-11-08 19:23:39 +05:30
### Alternative: `expect_next_instance_of`
Instead of writing:
```ruby
# Don't do this:
2019-02-15 15:39:39 +05:30
expect_any_instance_of(Project).to receive(:add_import_job)
2018-11-08 19:23:39 +05:30
```
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)
```
2017-08-17 22:00:37 +05:30
## Do not `rescue Exception`
See ["Why is it bad style to `rescue Exception => e` in Ruby?"][Exception].
_**Note:** This rule is [enforced automatically by
Rubocop](https://gitlab.com/gitlab-org/gitlab-ce/blob/8-4-stable/.rubocop.yml#L911-914)._
[Exception]: http://stackoverflow.com/q/10048173/223897
## Do not use inline JavaScript in views
Using the inline `:javascript` Haml filters comes with a
performance overhead. Using inline JavaScript is not a good way to structure your code and should be avoided.
_**Note:** We've [removed these two filters ](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/config/initializers/hamlit.rb )
in an initializer._
2016-06-02 11:05:42 +05:30
### Further reading
2017-08-17 22:00:37 +05:30
- Stack Overflow: [Why you should not write inline JavaScript ](http://programmers.stackexchange.com/questions/86589/why-should-i-avoid-inline-scripting )