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.
Do not assert against the absolute value of a sequence-generated attribute
Consider the following factory:
FactoryBot.definedofactory:labeldosequence(:title){|n|"label#{n}"}endend
Consider the following API spec:
require'rails_helper'describeAPI::Labelsdoit'creates a first label'docreate(:label)getapi("/projects/#{project.id}/labels",user)expect(response).tohave_http_status(200)expect(json_response.first['name']).toeq('label1')endit'creates a second label'docreate(:label)getapi("/projects/#{project.id}/labels",user)expect(response).tohave_http_status(200)expect(json_response.first['name']).toeq('label1')endend
When run, this spec doesn't do what we might expect:
1)API::API reproduce sequence issue creates a second labelFailure/Error: expect(json_response.first['name']).to eq('label1')expected:"label1"got:"label2"(compared using==)
That's because FactoryBot sequences are not reseted for each example.
Please remember that sequence-generated values exist only to avoid having to explicitly set attributes that have a uniqueness constraint when using a factory.
Solution
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, writingcreate(:label, title: 'foo')
is ok, butcreate(:label, title: 'label1')
is not.
Following is the fixed API spec:
require'rails_helper'describeAPI::Labelsdoit'creates a first label'docreate(:label,标题:'foo')getapi("/projects/#{project.id}/labels",user)expect(response).tohave_http_status(200)expect(json_response.first['name']).toeq('foo')endit'creates a second label'docreate(:label,标题:'bar')getapi("/projects/#{project.id}/labels",user)expect(response).tohave_http_status(200)expect(json_response.first['name']).toeq('bar')endend
rescue Exception
Do notSee"Why is it bad style torescue Exception => e
in Ruby?".
Note:This rule isenforced automatically by Rubocop.
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删除这两个过滤器sin an initializer.
Further reading
- Stack Overflow:Why you should not write inline JavaScript