diff options
author | Evan Read <eread@gitlab.com> | 2023-11-13 09:19:49 +0300 |
---|---|---|
committer | Evan Read <eread@gitlab.com> | 2023-11-13 09:19:49 +0300 |
commit | 8da1664026d2a2acec392337376b9d2181ea014f (patch) | |
tree | 5eace5a5cb08d03d9e3923a4491074da20954297 /spec/lib | |
parent | cb4874fdd22da8c15907883e17f4b9ce0963942e (diff) |
Use named subject in more RSpec tests
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/gitlab/docs/page_spec.rb | 22 | ||||
-rw-r--r-- | spec/lib/gitlab/navigation/category_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/navigation/doc_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/navigation/section_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/navigation_spec.rb | 4 | ||||
-rw-r--r-- | spec/lib/helpers/edit_on_gitlab_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/helpers/generic_spec.rb | 18 |
7 files changed, 30 insertions, 38 deletions
diff --git a/spec/lib/gitlab/docs/page_spec.rb b/spec/lib/gitlab/docs/page_spec.rb index a3ca9c12..22d9dc19 100644 --- a/spec/lib/gitlab/docs/page_spec.rb +++ b/spec/lib/gitlab/docs/page_spec.rb @@ -9,9 +9,7 @@ require 'gitlab/docs/element' require 'gitlab/docs/document' describe Gitlab::Docs::Page do - subject do - described_class.new('some/file.html') - end + subject(:page) { described_class.new('some/file.html') } context 'when file exists' do before do @@ -39,13 +37,13 @@ describe Gitlab::Docs::Page do describe '#links' do it 'collects links on a page' do - expect(subject.links.count).to eq 4 + expect(page.links.count).to eq 4 end end describe '#hrefs' do it 'collects all hrefs' do - expect(subject.hrefs).to match_array %w[../link.html#my-anchor + expect(page.hrefs).to match_array %w[../link.html#my-anchor #internal-anchor #internal-anchor-2 #utf-8-id-✔] @@ -54,31 +52,31 @@ describe Gitlab::Docs::Page do describe '#ids' do it 'collects all ids' do - expect(subject.ids).to match_array %w[internal-anchor + expect(page.ids).to match_array %w[internal-anchor utf-8-id-✔] end end describe '#has_anchor?' do it 'returns true when anchor exists on a page' do - expect(subject).to have_anchor('internal-anchor') + expect(page).to have_anchor('internal-anchor') end it 'returns false when anchors does not exist' do - expect(subject).not_to have_anchor('internal-anchor-2') + expect(page).not_to have_anchor('internal-anchor-2') end it 'returns true when UTF-8 encoded anchors are compared' do - expect(subject).to have_anchor('utf-8-id-✔') - expect(subject).to have_anchor('utf-8-id-%E2%9C%94') - expect(subject).to have_anchor('utf-8-id-%e2%9c%94') + expect(page).to have_anchor('utf-8-id-✔') + expect(page).to have_anchor('utf-8-id-%E2%9C%94') + expect(page).to have_anchor('utf-8-id-%e2%9c%94') end end end describe '#directory' do it 'returns base directory of a file' do - expect(subject.directory).to eq 'some' + expect(page.directory).to eq 'some' end end diff --git a/spec/lib/gitlab/navigation/category_spec.rb b/spec/lib/gitlab/navigation/category_spec.rb index f0424171..82550bbd 100644 --- a/spec/lib/gitlab/navigation/category_spec.rb +++ b/spec/lib/gitlab/navigation/category_spec.rb @@ -68,12 +68,10 @@ describe Gitlab::Navigation::Category do end describe '#children' do - subject { category.children } + subject(:category_children) { category.children } it 'returns a list of children' do - children = subject - - expect(children.first.title).to eq('Doc Title') + expect(category_children.first.title).to eq('Doc Title') end context 'when docs are empty' do diff --git a/spec/lib/gitlab/navigation/doc_spec.rb b/spec/lib/gitlab/navigation/doc_spec.rb index 600edb44..2f7a753c 100644 --- a/spec/lib/gitlab/navigation/doc_spec.rb +++ b/spec/lib/gitlab/navigation/doc_spec.rb @@ -67,12 +67,10 @@ describe Gitlab::Navigation::Doc do end describe '#children' do - subject { doc.children } + subject(:doc_children) { doc.children } it 'returns a list of children' do - children = subject - - expect(children.first.title).to eq('Doc Title') + expect(doc_children.first.title).to eq('Doc Title') end context 'when docs are empty' do diff --git a/spec/lib/gitlab/navigation/section_spec.rb b/spec/lib/gitlab/navigation/section_spec.rb index f71f60f2..a297e99e 100644 --- a/spec/lib/gitlab/navigation/section_spec.rb +++ b/spec/lib/gitlab/navigation/section_spec.rb @@ -60,12 +60,10 @@ describe Gitlab::Navigation::Section do end describe '#children' do - subject { section.children } + subject(:section_children) { section.children } it 'returns a list of children' do - children = subject - - expect(children.first.title).to eq('Category Title') + expect(section_children.first.title).to eq('Category Title') end context 'when categories are empty' do diff --git a/spec/lib/gitlab/navigation_spec.rb b/spec/lib/gitlab/navigation_spec.rb index 425a5428..5c341ea6 100644 --- a/spec/lib/gitlab/navigation_spec.rb +++ b/spec/lib/gitlab/navigation_spec.rb @@ -16,13 +16,13 @@ describe Gitlab::Navigation do end describe '#nav_items' do - subject { navigation.nav_items } + subject(:navigation_items) { navigation.nav_items } context 'when yaml configuration for project does not exist' do let(:item) { instance_double(Nanoc::Core::CompilationItemView, path: '/ee/user/README.html', identifier: instance_double(Nanoc::Core::Identifier, to_s: '/ee/user/README.md')) } it 'returns default sections' do - sections = subject[:sections] + sections = navigation_items[:sections] section = sections.first expect(section.title).to eq('Default Section') diff --git a/spec/lib/helpers/edit_on_gitlab_spec.rb b/spec/lib/helpers/edit_on_gitlab_spec.rb index a813e66c..eff9d437 100644 --- a/spec/lib/helpers/edit_on_gitlab_spec.rb +++ b/spec/lib/helpers/edit_on_gitlab_spec.rb @@ -14,7 +14,7 @@ RSpec.describe Nanoc::Helpers::EditOnGitLab do item.new(identifier, content_filename) end - subject { mock_class.edit_on_gitlab(mock_item, editor: editor) } + subject(:edit_on_gitlab) { mock_class.edit_on_gitlab(mock_item, editor: editor) } describe '#edit_on_gitlab' do using RSpec::Parameterized::TableSyntax @@ -36,14 +36,14 @@ RSpec.describe Nanoc::Helpers::EditOnGitLab do with_them do it 'returns correct url for identifier and editor' do - expect(subject).to eq(expected_url) + expect(edit_on_gitlab).to eq(expected_url) end end context 'with unknown editor' do let(:editor) { :word } - it { expect { subject }.to raise_error("Unknown editor: word") } + it { expect { edit_on_gitlab }.to raise_error("Unknown editor: word") } end end end diff --git a/spec/lib/helpers/generic_spec.rb b/spec/lib/helpers/generic_spec.rb index ae4ead59..2a1b7f4d 100644 --- a/spec/lib/helpers/generic_spec.rb +++ b/spec/lib/helpers/generic_spec.rb @@ -22,7 +22,7 @@ RSpec.describe Nanoc::Helpers::Generic do describe '#docs_section' do using RSpec::Parameterized::TableSyntax - subject { mock_class.docs_section(mock_item.title, mock_item.path) } + subject(:docs_section) { mock_class.docs_section(mock_item.title, mock_item.path) } where(:path, :title, :expected_section_title) do "/ee/tutorials/" | "Learn GitLab with tutorials" | "Tutorials" @@ -35,7 +35,7 @@ RSpec.describe Nanoc::Helpers::Generic do with_them do it "returns the section title for the given path" do - expect(subject).to eq(expected_section_title) + expect(docs_section).to eq(expected_section_title) end end end @@ -214,7 +214,7 @@ RSpec.describe Nanoc::Helpers::Generic do describe '#docs_breadcrumb_list' do using RSpec::Parameterized::TableSyntax - subject { mock_class.docs_breadcrumb_list(mock_item.path) } + subject(:docs_breadcrumb_list) { mock_class.docs_breadcrumb_list(mock_item.path) } where(:path, :expected_breadcrumb_list) do "/ee/tutorials/" | "Tutorials" @@ -226,13 +226,13 @@ RSpec.describe Nanoc::Helpers::Generic do with_them do it "returns the breadcrumb trail for the given path" do - expect(subject).to eq(expected_breadcrumb_list) + expect(docs_breadcrumb_list).to eq(expected_breadcrumb_list) end end end describe '#get_release_dates' do - subject { mock_class.get_release_dates } + subject(:release_dates) { mock_class.get_release_dates } valid_yaml_content = "- version: '18.0'\n date: '2025-05-15'" invalid_yaml_content = "version: '18.0'date: 2025-05-15'" @@ -240,25 +240,25 @@ RSpec.describe Nanoc::Helpers::Generic do it 'returns a JSON array when the YAML is valid' do allow(Net::HTTP).to receive(:get_response).and_return(Net::HTTPSuccess.new('1.1', '200', 'OK')) allow_any_instance_of(Net::HTTPSuccess).to receive(:body).and_return(valid_yaml_content) - expect(subject).to be_a(String) + expect(release_dates).to be_a(String) end it 'returns an empty JSON array when the YAML is invalid' do allow(Net::HTTP).to receive(:get_response).and_return(Net::HTTPSuccess.new('1.1', '200', 'OK')) allow_any_instance_of(Net::HTTPSuccess).to receive(:body).and_return(invalid_yaml_content) allow(mock_class).to receive(:warn).with('Error getting release dates - (<unknown>): did not find expected key while parsing a block mapping at line 1 column 1') - expect(subject).to eq("[]") + expect(release_dates).to eq("[]") end it 'returns an empty JSON array on HTTP error' do allow(Net::HTTP).to receive(:get_response).and_return(Net::HTTPServerError.new('1.1', '500', 'Internal Server Error')) - expect(subject).to eq("[]") + expect(release_dates).to eq("[]") end it 'returns an empty JSON array on other errors' do allow(Net::HTTP).to receive(:get_response).and_raise(StandardError.new('Some error message')) allow(mock_class).to receive(:warn).with('Error getting release dates - Some error message') - expect(subject).to eq("[]") + expect(release_dates).to eq("[]") end end |