Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
- Use time helpers in event_test.rb
- Add WebMock
- Refactor GroupsBreadcrumbsTest
- Stub requests to maps.googleapis.com
- Extract address utility method to test_helper.rb
- Extract fake_event to test_helper.rb
- Refactor NotificationsTest
- Refactor GroupsShowTest
- Extract groups utility methods to test_helper.rb
- Refactor NotificationMailerTest
- Refactor GroupsRolesTest
- Extract attach_valid_image to test_helper.rb
- Extract fill_in_description to test_helper.rb
- Refactor UsersSignUpTest
- Refactor GroupsSearchTest
- Remove unnecessary GroupMembershipTest
- Extract upload_valid_image to test_helper.rb
  • Loading branch information
lujanfernaud committed Apr 13, 2018
1 parent 73bb002 commit a7ff2a8
Show file tree
Hide file tree
Showing 19 changed files with 356 additions and 384 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ group :test do
gem 'guard', '2.14.0'
gem 'guard-minitest', '2.4.6'
gem 'minitest-reporters', '1.1.14'
gem 'webmock', '~> 3.3'
gem 'simplecov', :require => false
end

Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ GEM
country_select (3.1.1)
countries (~> 2.0)
sort_alphabetical (~> 1.0)
crack (0.4.3)
safe_yaml (~> 1.0.0)
crass (1.0.3)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
Expand Down Expand Up @@ -282,6 +284,7 @@ GEM
guard-minitest (2.4.6)
guard-compat (~> 1.2)
minitest (>= 3.0)
hashdiff (0.3.7)
http_parser.rb (0.6.0)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -413,6 +416,7 @@ GEM
ruby-graphviz (1.2.3)
ruby-progressbar (1.9.0)
ruby_dep (1.5.0)
safe_yaml (1.0.4)
sass (3.5.6)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
Expand Down Expand Up @@ -475,6 +479,10 @@ GEM
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
webmock (3.3.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
Expand Down Expand Up @@ -531,6 +539,7 @@ DEPENDENCIES
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
webmock (~> 3.3)
will_paginate (~> 3.1, >= 3.1.6)

RUBY VERSION
Expand Down
8 changes: 2 additions & 6 deletions test/controllers/events_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class EventsControllerTest < ActionDispatch::IntegrationTest
sign_in(@user)

patch group_event_url(@group, @event),
params: { event: { image: sample_image } }
params: { event: { image: upload_valid_image } }

assert_redirected_to group_event_url(@group, @event)
end
Expand All @@ -87,12 +87,8 @@ def event_params
website: "www.event.com",
start_date: Time.zone.now + 6.days,
end_date: Time.zone.now + 1.week,
image: sample_image
image: upload_valid_image
}
}
end

def sample_image
fixture_file_upload("test/fixtures/files/sample.jpeg", "image/jpeg")
end
end
4 changes: 0 additions & 4 deletions test/controllers/groups_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ class GroupsControllerTest < ActionDispatch::IntegrationTest

private

def upload_valid_image
fixture_file_upload("test/fixtures/files/sample.jpeg", "image/jpeg")
end

def group_params
{
name: "Test group",
Expand Down
38 changes: 8 additions & 30 deletions test/decorators/event_decorator_test.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,27 @@
require 'test_helper'

class EventDecoratorTest < ActiveSupport::TestCase
def setup
stub_requests_to_googleapis
end

test "#full_address returns full address without country" do
event = fake_event
event = fake_event_decorator
full_address = "Obento, Matsubara-dori, 8, Kyoto, 6050856"

assert_equal event.full_address, full_address
end

test "#short_address returns place name and city" do
event = fake_event
event = fake_event_decorator
short_address = "Obento, Kyoto"

assert_equal event.short_address, short_address
end

private

def fake_event(params = {})
@fake_event ||= EventDecorator.new(Event.new(
group: groups(:two),
title: params[:title] || "Test event",
description: params[:description] || Faker::Lorem.paragraph,
website: params[:website] || "",
start_date: params[:start_date] || Time.zone.now + 6.days,
end_date: params[:end_date] || Time.zone.now + 1.week,
image: params[:image] || image,
organizer: users(:phil),
address_attributes: address(params)
))
end

def image
File.open(Rails.root.join('test/fixtures/files/sample.jpeg'))
end

def address(params = {})
{
place_name: params[:place_name] || "Obento",
street1: params[:street1] || "Matsubara-dori, 8",
street2: params[:street2] || "",
city: params[:city] || "Kyoto",
state: params[:state] || "",
post_code: params[:post_code] || "6050856",
country: params[:country] || "Japan"
}
def fake_event_decorator(params = {})
@fake_event_decorator ||= EventDecorator.new(fake_event(params))
end
end
56 changes: 24 additions & 32 deletions test/integration/events_creation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)
fill_in_valid_address
fill_in_valid_dates
attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_valid
Expand All @@ -44,11 +44,11 @@ def setup
end

fill_in "Title", with: "T"
fill_in_description
fill_in_description(@event.description)

fill_in_valid_address
fill_in_valid_dates
attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -67,7 +67,7 @@ def setup

fill_in_valid_address
fill_in_valid_dates
attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -81,7 +81,7 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)

fill_in "Address 1", with: ""
fill_in "Address 2", with: @event.street2
Expand All @@ -91,7 +91,7 @@ def setup
select "Spain", from: "Country"

fill_in_valid_dates
attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -105,7 +105,7 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)

fill_in "Address 1", with: @event.street1
fill_in "Address 2", with: @event.street2
Expand All @@ -115,7 +115,7 @@ def setup
select "Spain", from: "Country"

fill_in_valid_dates
attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -129,7 +129,7 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)

fill_in "Address 1", with: @event.street1
fill_in "Address 2", with: @event.street2
Expand All @@ -139,7 +139,7 @@ def setup
select "Spain", from: "Country"

fill_in_valid_dates
attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -153,7 +153,7 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)

fill_in "Address 1", with: @event.street1
fill_in "Address 2", with: @event.street2
Expand All @@ -162,7 +162,7 @@ def setup
select "", from: "Country"

fill_in_valid_dates
attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -176,13 +176,13 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)
fill_in_valid_address

select_date_and_time 1.week.ago, from: "event_start_date"
select_date_and_time @event.end_date, from: "event_end_date"

attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -196,13 +196,13 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)
fill_in_valid_address

select_date_and_time @event.start_date, from: "event_start_date"
select_date_and_time 1.week.ago, from: "event_end_date"

attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -216,13 +216,13 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)
fill_in_valid_address

select_date_and_time nil, from: "event_start_date"
select_date_and_time @event.end_date, from: "event_end_date"

attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -236,13 +236,13 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)
fill_in_valid_address

select_date_and_time @event.start_date, from: "event_start_date"
select_date_and_time nil, from: "event_end_date"

attach_valid_image
attach_valid_image_for "event_image"

click_on_create_event
assert_invalid
Expand All @@ -256,7 +256,7 @@ def setup
click_on "Create event"
end

fill_in_valid_information
fill_in_valid_information(@event.description)
fill_in_valid_address
fill_in_valid_dates

Expand All @@ -266,13 +266,9 @@ def setup

private

def fill_in_valid_information
def fill_in_valid_information(event_description)
fill_in "Title", with: @event.title
fill_in_description
end

def fill_in_description(description = @event.description)
find("trix-editor").click.set(description)
fill_in_description(event_description)
end

def fill_in_valid_address
Expand All @@ -289,10 +285,6 @@ def fill_in_valid_dates
select_date_and_time @event.end_date, from: "event_end_date"
end

def attach_valid_image
attach_file "event_image", "test/fixtures/files/sample.jpeg"
end

def click_on_create_event
within "form" do
click_on "Create event"
Expand Down
Loading

0 comments on commit a7ff2a8

Please sign in to comment.