Skip to content

Commit

Permalink
Avoid stubbing in the banner integration test
Browse files Browse the repository at this point in the history
Integration tests shouldn't use stubbing at all. Instead, create
real files on the filesystem. This change involves making the path
to the banner templates configurable, with the default being in
"app/views/notifications".
  • Loading branch information
benilovj committed Sep 16, 2015
1 parent 6f3ed65 commit f1338fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
8 changes: 6 additions & 2 deletions lib/notification_file_lookup.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class NotificationFileLookup
def initialize(banner_file_location = "#{Rails.root}/app/views/notifications")
@banner_file_location = banner_file_location
end

def banner
@banner_file ||= identify_banner_file
end
Expand All @@ -10,8 +14,8 @@ def banner=(file)
private

def identify_banner_file
red = File.read("#{Rails.root}/app/views/notifications/banner_red.erb").strip
green = File.read("#{Rails.root}/app/views/notifications/banner_green.erb").strip
red = File.read(File.join(@banner_file_location, "banner_red.erb")).strip
green = File.read(File.join(@banner_file_location, "banner_green.erb")).strip

case
when red.present?
Expand Down
28 changes: 13 additions & 15 deletions test/integration/notifications_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ class NotificationsTest < ActionDispatch::IntegrationTest
end

context "banner notifications" do
teardown do
clean_up_test_files
end

context "given view files are empty" do
setup do
File.stubs(:read).with("#{Rails.root}/app/views/notifications/banner_green.erb")
.returns('')
File.stubs(:read).with("#{Rails.root}/app/views/notifications/banner_red.erb")
.returns('')
create_test_file(filename: "banner_green.erb", content: '')
create_test_file(filename: "banner_red.erb", content: '')

Static.banner = NotificationFileLookup.new.banner
Static.banner = NotificationFileLookup.new(location_of_test_files).banner
end

should "not show a banner notification on the page" do
Expand All @@ -38,12 +40,10 @@ class NotificationsTest < ActionDispatch::IntegrationTest

context "given view files are present for a green notification" do
setup do
File.stubs(:read).with("#{Rails.root}/app/views/notifications/banner_green.erb")
.returns('<p>Everything is fine</p>')
File.stubs(:read).with("#{Rails.root}/app/views/notifications/banner_red.erb")
.returns('')
create_test_file(filename: "banner_green.erb", content: '<p>Everything is fine</p>')
create_test_file(filename: "banner_red.erb", content: '')

Static.banner = NotificationFileLookup.new.banner
Static.banner = NotificationFileLookup.new(location_of_test_files).banner
end

should "show a banner notification on the page" do
Expand All @@ -55,12 +55,10 @@ class NotificationsTest < ActionDispatch::IntegrationTest

context "given view files are present for a red notification" do
setup do
File.stubs(:read).with("#{Rails.root}/app/views/notifications/banner_green.erb")
.returns('')
File.stubs(:read).with("#{Rails.root}/app/views/notifications/banner_red.erb")
.returns('<p>Everything is not fine</p>')
create_test_file(filename: "banner_green.erb", content: '')
create_test_file(filename: "banner_red.erb", content: '<p>Everything is not fine</p>')

Static.banner = NotificationFileLookup.new.banner
Static.banner = NotificationFileLookup.new(location_of_test_files).banner
end

should "show a banner notification on the page" do
Expand Down
16 changes: 16 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,19 @@
require 'test/unit'
require 'rails/test_help'
require 'mocha/mini_test'

class ActiveSupport::TestCase
def create_test_file(filename:, content:)
FileUtils.mkdir_p location_of_test_files
full_path = File.join(location_of_test_files, filename)
File.open(full_path, "w") {|f| f << content }
end

def location_of_test_files
Rails.root.join("tmp", "test")
end

def clean_up_test_files
FileUtils.rm_r location_of_test_files
end
end

0 comments on commit f1338fd

Please sign in to comment.