From f7e54bc9f8f89ad0168e5289ec3f555ccfabf16b Mon Sep 17 00:00:00 2001 From: Cassio Santana Date: Mon, 18 Sep 2023 22:42:07 -0300 Subject: [PATCH] test(book): Refactor tests --- spec/features/book_filter_spec.rb | 2 +- spec/routing/books_routing_spec.rb | 38 +++++++++---------- .../{ => admin}/books/edit.html.erb_spec.rb | 12 +++--- .../{ => admin}/books/index.html.erb_spec.rb | 8 ++-- .../{ => admin}/books/new.html.erb_spec.rb | 8 ++-- .../{ => admin}/books/show.html.erb_spec.rb | 10 ++--- 6 files changed, 39 insertions(+), 39 deletions(-) rename spec/views/{ => admin}/books/edit.html.erb_spec.rb (82%) rename spec/views/{ => admin}/books/index.html.erb_spec.rb (63%) rename spec/views/{ => admin}/books/new.html.erb_spec.rb (79%) rename spec/views/{ => admin}/books/show.html.erb_spec.rb (76%) diff --git a/spec/features/book_filter_spec.rb b/spec/features/book_filter_spec.rb index 87d1f413..93413675 100644 --- a/spec/features/book_filter_spec.rb +++ b/spec/features/book_filter_spec.rb @@ -17,7 +17,7 @@ end before do - visit books_path + visit admin_books_path end context "when initializing the page" do diff --git a/spec/routing/books_routing_spec.rb b/spec/routing/books_routing_spec.rb index d1d0dd9a..39b0dead 100644 --- a/spec/routing/books_routing_spec.rb +++ b/spec/routing/books_routing_spec.rb @@ -1,39 +1,39 @@ # frozen_string_literal: true -require 'rails_helper' +require "rails_helper" -RSpec.describe BooksController, type: :routing do - describe 'routing' do - it 'routes to #index' do - expect(get: '/books').to route_to('books#index') +RSpec.describe Admin::BooksController, type: :routing do + describe "routing" do + it "routes to #index" do + expect(get: "admin/books").to route_to("admin/books#index") end - it 'routes to #new' do - expect(get: '/books/new').to route_to('books#new') + it "routes to #new" do + expect(get: "admin/books/new").to route_to("admin/books#new") end - it 'routes to #show' do - expect(get: '/books/1').to route_to('books#show', id: '1') + it "routes to #show" do + expect(get: "admin/books/1").to route_to("admin/books#show", id: "1") end - it 'routes to #edit' do - expect(get: '/books/1/edit').to route_to('books#edit', id: '1') + it "routes to #edit" do + expect(get: "admin/books/1/edit").to route_to("admin/books#edit", id: "1") end - it 'routes to #create' do - expect(post: '/books').to route_to('books#create') + it "routes to #create" do + expect(post: "admin/books").to route_to("admin/books#create") end - it 'routes to #update via PUT' do - expect(put: '/books/1').to route_to('books#update', id: '1') + it "routes to #update via PUT" do + expect(put: "admin/books/1").to route_to("admin/books#update", id: "1") end - it 'routes to #update via PATCH' do - expect(patch: '/books/1').to route_to('books#update', id: '1') + it "routes to #update via PATCH" do + expect(patch: "admin/books/1").to route_to("admin/books#update", id: "1") end - it 'routes to #destroy' do - expect(delete: '/books/1').to route_to('books#destroy', id: '1') + it "routes to #destroy" do + expect(delete: "admin/books/1").to route_to("admin/books#destroy", id: "1") end end end diff --git a/spec/views/books/edit.html.erb_spec.rb b/spec/views/admin/books/edit.html.erb_spec.rb similarity index 82% rename from spec/views/books/edit.html.erb_spec.rb rename to spec/views/admin/books/edit.html.erb_spec.rb index c622d432..71d0a7c7 100644 --- a/spec/views/books/edit.html.erb_spec.rb +++ b/spec/views/admin/books/edit.html.erb_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -RSpec.describe "books/edit", type: :view do +RSpec.describe "admin/books/edit", type: :view do let(:authors) { create_list(:author, 3) } let(:not_associated_assemblies) { create_list(:assembly, 3) } let(:associated_assemblies) { create_list(:assembly, 4) } @@ -16,11 +16,11 @@ end it "render the page title" do - expect_page_title("Editing book") + expect(rendered).to have_selector("h1", text: "Editing book") end it "renders the book form" do - form = "form[action='#{book_path(book)}'][method='post']" + form = "form[action='#{admin_book_path(book)}'][method='post']" title = "input[type='text'][name='book[title]'][value='#{book.title}']" datetime = "input[type='datetime-local'][name='book[published_at]']" isbn = "input[type='text'][name='book[isbn]'][value='#{book.isbn}']" @@ -44,14 +44,14 @@ datetime_input_value = Capybara.string(rendered).find(datetime).value expect(DateTime.parse(datetime_input_value).utc.iso8601).to eq(book.published_at.utc.iso8601) - expect_submit_button("Update Book") + expect(rendered).to have_button("Update Book") end it "render the show book link" do - expect_link_to_show(book) + expect(rendered).to have_link("Show this book", href: admin_book_path(book)) end it "render the back link" do - expect_link_back_to("books") + expect(rendered).to have_link("Back to books", href: admin_books_path) end end diff --git a/spec/views/books/index.html.erb_spec.rb b/spec/views/admin/books/index.html.erb_spec.rb similarity index 63% rename from spec/views/books/index.html.erb_spec.rb rename to spec/views/admin/books/index.html.erb_spec.rb index 5732280b..115b86e7 100644 --- a/spec/views/books/index.html.erb_spec.rb +++ b/spec/views/admin/books/index.html.erb_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -RSpec.describe "books/index", type: :view do +RSpec.describe "admin/books/index", type: :view do let(:author) { create(:author) } let(:books) { create_list(:book, 5, author:) } @@ -13,17 +13,17 @@ end it "render the page title" do - expect_page_title("Books") + expect(rendered).to have_selector("h1", text: "Books") end it "render a list of books" do books.each do |book| expect(rendered).to have_text("Title: #{book.title}", normalize_ws: true) - expect(rendered).to have_link("Show this book", href: book_path(book)) + expect(rendered).to have_link("Show this book", href: admin_book_path(book)) end end it "render a link to new book" do - expect_link_to_new("book") + expect(rendered).to have_link("New book", href: new_admin_book_path) end end diff --git a/spec/views/books/new.html.erb_spec.rb b/spec/views/admin/books/new.html.erb_spec.rb similarity index 79% rename from spec/views/books/new.html.erb_spec.rb rename to spec/views/admin/books/new.html.erb_spec.rb index a805992f..76ee9eba 100644 --- a/spec/views/books/new.html.erb_spec.rb +++ b/spec/views/admin/books/new.html.erb_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -RSpec.describe "books/new", type: :view do +RSpec.describe "admin/books/new", type: :view do let(:book) { build(:book) } let(:author1) { create(:author, name: "Author 1") } let(:author2) { create(:author, name: "Author 2") } @@ -16,11 +16,11 @@ end it "render the page title" do - expect_page_title("New book") + expect(rendered).to have_selector("h1", text: "New book") end it "render the book form" do - expect(rendered).to have_selector("form[action='#{books_path}'][method='post']") + expect(rendered).to have_selector("form[action='#{admin_books_path}'][method='post']") expect(rendered).to have_selector("input[type='text'][name='book[title]']") expect(rendered).to have_selector("input[type='datetime-local'][name='book[published_at]']") expect(rendered).to have_selector("input[type='text'][name='book[isbn]']") @@ -31,6 +31,6 @@ end it "render the back link" do - expect_link_back_to("books") + expect(rendered).to have_link("Back to books", href: admin_books_path) end end diff --git a/spec/views/books/show.html.erb_spec.rb b/spec/views/admin/books/show.html.erb_spec.rb similarity index 76% rename from spec/views/books/show.html.erb_spec.rb rename to spec/views/admin/books/show.html.erb_spec.rb index 193020ca..d00a8fb3 100644 --- a/spec/views/books/show.html.erb_spec.rb +++ b/spec/views/admin/books/show.html.erb_spec.rb @@ -2,10 +2,10 @@ require "rails_helper" -RSpec.describe "books/show", type: :view do +RSpec.describe "admin/books/show", type: :view do let(:assemblies) { create_list(:assembly, 3) } let(:author) { create(:author) } - let(:book) { create(:book, author: author, assemblies: assemblies) } + let(:book) { create(:book, author:, assemblies:) } before(:each) do assign(:book, book) @@ -27,15 +27,15 @@ context "rendering links and button" do it "render the link to edit book" do - expect_link_to_edit(book) + expect(rendered).to have_link("Edit this book", href: edit_admin_book_path(book)) end it "render the link to all books" do - expect_link_back_to("books") + expect(rendered).to have_link("Back to books", href: admin_books_path) end it "render the button to destroy book" do - expect_submit_button("Destroy this book") + expect(rendered).to have_button("Destroy this book") end end end