Skip to content

Commit

Permalink
test(book): Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cassiosantana committed Sep 19, 2023
1 parent 3366f7c commit f7e54bc
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion spec/features/book_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end

before do
visit books_path
visit admin_books_path
end

context "when initializing the page" do
Expand Down
38 changes: 19 additions & 19 deletions spec/routing/books_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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}']"
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:) }

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand All @@ -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]']")
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

0 comments on commit f7e54bc

Please sign in to comment.