Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '~> 2.5.3'

group :test do
gem "minitest-spec-rails"
gem "capybara-webkit"
gem "factory_girl_rails", '4.1.0'
gem "database_cleaner"
gem "webmock", require: false
gem "mocha", require: false
gem 'rspec-rails', '~> 3.0'
gem 'capybara', '~> 2.4.1'
gem 'factory_girl_rails'
gem 'database_cleaner'
gem 'webmock', require: false
end

group :production do
Expand Down
37 changes: 22 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@ GEM
multi_json
arel (6.0.3)
builder (3.2.2)
capybara (2.7.0)
addressable
capybara (2.4.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
capybara-webkit (1.10.1)
capybara (>= 2.3.0, < 2.8.0)
json
crack (0.4.3)
safe_yaml (~> 1.0.0)
database_cleaner (1.5.1)
diff-lcs (1.2.5)
domain_name (0.5.20160310)
unf (>= 0.0.5, < 1.0.0)
erubis (2.7.0)
Expand Down Expand Up @@ -94,15 +91,9 @@ GEM
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
metaclass (0.0.4)
mime-types (2.99.1)
mini_portile2 (2.0.0)
minitest (5.8.4)
minitest-spec-rails (5.3.0)
minitest (~> 5.0)
rails (>= 4.1)
mocha (1.1.0)
metaclass (~> 0.0.1)
multi_json (1.11.2)
multi_xml (0.5.5)
multipart-post (2.0.0)
Expand Down Expand Up @@ -175,6 +166,23 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rspec-core (3.4.4)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-rails (3.4.2)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
safe_yaml (1.0.4)
sass (3.2.19)
sass-rails (4.0.5)
Expand Down Expand Up @@ -214,15 +222,13 @@ PLATFORMS

DEPENDENCIES
airbrake (~> 4.0.0)
capybara-webkit
capybara (~> 2.4.1)
database_cleaner
factory_girl_rails (= 4.1.0)
factory_girl_rails
formtastic
googlebooks
has_scope
jquery-rails
minitest-spec-rails
mocha
omniauth-google-oauth2
openlibrary
paper_trail (~> 3.0.5)
Expand All @@ -232,6 +238,7 @@ DEPENDENCIES
rack-ssl-enforcer
rails (= 4.2.6)
rails_12factor
rspec-rails (~> 3.0)
sass-rails (~> 4.0.3)
uglifier (~> 2.5.3)
webmock
Expand Down
2 changes: 1 addition & 1 deletion app/views/copies/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<table class="history">
<% @previous_loans.each do |loan| %>
<tr>
<td><%= loan.loan_date.strftime("%e %B %Y") if loan.loan_date %> - <%= loan.return_date.strftime("%e %B %Y") if loan.return_date %></td>
<td><%= l loan.loan_date, format: :short_date if loan.loan_date %> - <%= l loan.return_date, format: :short_date if loan.return_date %></td>
<td>
<%= link_to loan.user.name, user_path(loan.user) %>
<% if loan.returned_by_another_user? %>
Expand Down
16 changes: 16 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rspec-core", "rspec")
1 change: 0 additions & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
config.after_initialize do
PaperTrail.enabled = false
end
config.minitest_spec_rails.mini_shoulda = true

config.eager_load = false

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :developer, { :fields => [:id, :name, :nickname], :uid_field => :id } if Rails.env.development?
provider :developer, { :fields => [:id, :name, :email], :uid_field => :id } if Rails.env.development?
provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"],
{
name: "google",
Expand Down
4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
hello: "Hello world"
time:
formats:
short_date: '%-d %B %Y'
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions spec/factories/loan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FactoryGirl.define do
factory :loan do
user
copy
state "on_loan"
sequence(:loan_date) {|n|
Date.parse('2000-01-01') + n.week
}
return_date nil

trait :returned do
state :returned
sequence(:return_date) {|n|
Date.parse('2001-01-01') + n.week
}
end

factory :returned_loan, traits: [:returned]
end
end
4 changes: 3 additions & 1 deletion test/factories/user.rb → spec/factories/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FactoryGirl.define do
factory :user do
name 'Winston Smith-Churchill'
sequence(:name) {|n|
"User#{n}"
}
sequence(:email) {|n|
"user#{n}@example.org"
}
Expand Down
73 changes: 73 additions & 0 deletions spec/features/borrowing_books_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'rails_helper'

RSpec.describe 'borrowing books', type: :feature do
let(:book) { create(:book) }
let(:copy) { create(:copy, book: book) }

before(:each) { sign_in_user }

it 'borrows a copy that is available' do
visit copy_path(copy)

expect(page).to have_content('Available')

click_on 'Borrow'

expect(page).to have_content('On loan to you')
end

it 'cannot borrow a copy that is on loan' do
copy.borrow(create(:user))
visit copy_path(copy)

expect(page).to have_content('On loan')
expect(page).to_not have_link('Borrow')
end

it 'returns a copy on loan to the current user' do
copy.borrow(signed_in_user)
visit copy_path(copy)

expect(page).to have_content('On loan to you')

click_on 'Return'

expect(page).to have_content('Available')
end

it 'returns a copy on loan to another user' do
other_user = create(:user)
copy.borrow(other_user)
visit copy_path(copy)

expect(page).to have_content("On loan to #{other_user.name}")

click_on 'Return'

expect(page).to have_content('Available')
expect(page).to have_content("returned by #{signed_in_user.name}")
end

it 'lists previous loans of a copy' do
previous_user = create(:user)

loans = create_list(:returned_loan, 5, copy: copy, user: previous_user)

visit copy_path(copy)

entries = page.all('table.history tr').map {|row|
cells = row.all('th, td')
cells.map(&:text).map(&:strip)
}

expect(entries).to contain_exactly(
*loans.map {|loan|
[
"#{I18n.localize loan.loan_date, format: :short_date} - #{I18n.localize loan.return_date, format: :short_date}",
loan.user.name,
]
}
)
end

end
62 changes: 62 additions & 0 deletions spec/features/browsing_books_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'rails_helper'

RSpec.describe 'browsing books', type: :feature do
let!(:book) { create(:book) }
before(:each) { sign_in_user }

it 'displays the book details' do
visit "/books/#{book.id}"

within ".cover" do
expect(page).to have_selector("img[src='http://bks0.books.google.co.uk/books?id=#{book.google_id}&printsec=frontcover&img=1&zoom=1&edge=none&source=gbs_api']")
end

within ".title" do
expect(page).to have_content(book.title)
expect(page).to have_content("by #{book.author}")
end
end

it 'lists the copies of the book' do
visit "/books/#{book.id}"

expect(page).to have_content("1 copy")
within ".copies li" do
expect(page).to have_link("#1", :href => "/copy/#{book.copies.first.to_param}")
expect(page).to have_content("Available to borrow")
expect(page).to have_link("Borrow", :href => "/copy/#{book.copies.first.to_param}/borrow")
end
end

it 'lists previous versions of the book' do
previous_editor = create(:user)

with_versioning do
PaperTrail.whodunnit = previous_editor.id.to_s
book.update_attributes!(title: 'Goodnight Mister Tom')

visit "/books/#{book.id}"
click_on 'See revision history'

expect(page).to have_selector('table.history')

entries = page.all('table.history tr').map {|row|
cells = row.all('td')
cells.map(&:text).map(&:strip)
}

expect(entries).to contain_exactly(
[ previous_editor.name, "less than a minute ago", "Title: Goodnight Mister Tom" ]
)
end
end

it 'shows a message when no previous book versions exist' do
visit "/books/#{book.id}"
click_on "See revision history"

assert page.has_content?(book.title)
assert page.has_content?("No changes have been made to this book yet.")
end

end
41 changes: 41 additions & 0 deletions spec/features/start_page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rails_helper'

RSpec.describe 'start page', type: :feature do
before(:each) { sign_in_user }

it 'tells the user how many books they have on loan' do
create_list(:loan, 5, user: signed_in_user)

visit root_path

expect(page).to have_content("Welcome, #{signed_in_user.name}!")
expect(page).to have_content('You have 5 books on loan')
end

it 'lists recently added books' do
create_list(:copy, 10)
recent_copy = create(:copy)

visit root_path

within '.recently-added' do
expect(page).to have_link(recent_copy.book.title,
href: copy_path(recent_copy))
end
end

it 'list recent loans' do
loans = create_list(:loan, 5)

visit root_path

within '.recent-activity' do
loans.reverse.each_with_index do |loan, i|
within "li:nth-of-type(#{i+1})" do
expect(page).to have_link(loan.user.name, href: user_path(loan.user))
expect(page).to have_link("##{loan.copy.book_reference}: #{loan.book.title}", href: copy_path(loan.copy))
end
end
end
end
end
20 changes: 20 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
config.use_transactional_fixtures = true

config.infer_spec_type_from_file_location!
end
Loading