Skip to content

Commit

Permalink
Updates and cleanups to the tests, mostly the test_helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshk committed Jun 21, 2011
1 parent 63af90e commit 3ca2a8c
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 210 deletions.
70 changes: 35 additions & 35 deletions test/factories.rb
@@ -1,40 +1,40 @@
Factory.define :repository do |f|
f.name 'minimal'
f.owner_name 'svenfuchs'
f.owner_email 'svenfuchs@artweb-design.de'
f.url { |r| "http://github.com/#{r.owner_name}/#{r.name}" }
f.last_duration 60
f.created_at { |r| Time.utc(2011, 01, 30, 5, 25) }
f.updated_at { |r| r.created_at + 5.minutes }
end
Factory.define :repository do |f|
f.name 'minimal'
f.owner_name 'svenfuchs'
f.owner_email 'svenfuchs@artweb-design.de'
f.url { |r| "http://github.com/#{r.owner_name}/#{r.name}" }
f.last_duration 60
f.created_at { |r| Time.utc(2011, 01, 30, 5, 25) }
f.updated_at { |r| r.created_at + 5.minutes }
end

Factory.define :build do |f|
f.repository { Repository.first || Factory(:repository) }
f.number '1'
f.commit '62aae5f70ceee39123ef'
f.branch 'master'
f.message 'the commit message'
f.committer_name 'Sven Fuchs'
f.committer_email 'svenfuchs@artweb-design.de'
end
Factory.define :build do |f|
f.repository { Repository.first || Factory(:repository) }
f.number '1'
f.commit '62aae5f70ceee39123ef'
f.branch 'master'
f.message 'the commit message'
f.committer_name 'Sven Fuchs'
f.committer_email 'svenfuchs@artweb-design.de'
end

Factory.define :running_build, :parent => :build do |f|
f.started_at { Time.now }
end
Factory.define :running_build, :parent => :build do |f|
f.started_at { Time.now }
end

Factory.define :successfull_build, :parent => :build do |f|
f.status 0
f.finished_at { Time.now }
end
Factory.define :successfull_build, :parent => :build do |f|
f.status 0
f.finished_at { Time.now }
end

Factory.define :broken_build, :parent => :build do |f|
f.status 1
f.started_at { Time.now }
f.finished_at { Time.now }
end
Factory.define :broken_build, :parent => :build do |f|
f.status 1
f.started_at { Time.now }
f.finished_at { Time.now }
end

Factory.define :user do |f|
f.name 'Sven Fuchs'
f.login 'svenfuchs'
f.email 'sven@fuchs.com'
end
Factory.define :user do |f|
f.name 'Sven Fuchs'
f.login 'svenfuchs'
f.email 'sven@fuchs.com'
end
2 changes: 1 addition & 1 deletion test/integration/application_controller_test.rb
@@ -1,4 +1,4 @@
require 'test_helper_rails'
require 'test_helper'

class ApplicationControllerTest < ActionDispatch::IntegrationTest
def setup
Expand Down
10 changes: 6 additions & 4 deletions test/integration/builds_controller_test.rb
@@ -1,16 +1,18 @@
require 'test_helper_rails'
require 'test_helper'

class BuildsControllerTest < ActionDispatch::IntegrationTest
include GithubApiTestHelper
include TestHelpers::GithubApiTestHelper

attr_reader :channel, :build

def setup
super

flush_redis

@build = Factory(:build).reload
@channel = Mocks::Channel.new
@build = Factory(:build).reload
@channel = TestHelpers::Mocks::Channel.new

Pusher.stubs(:[]).returns(channel)
end

Expand Down
2 changes: 1 addition & 1 deletion test/integration/route_redirection_test.rb
@@ -1,4 +1,4 @@
require 'test_helper_rails'
require 'test_helper'

class RouteRedirectionTest < ActionDispatch::IntegrationTest
fixtures :all
Expand Down
43 changes: 24 additions & 19 deletions test/test_helper.rb
@@ -1,36 +1,41 @@
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

begin
require 'ruby-debug'
rescue LoadError => e
puts e.message
end
require 'bundler/setup'

require 'active_record'
require 'test/unit'
require 'test_declarative'
require 'mocha'
require 'fakeredis'

require 'travis'

# load all the test helpers
Dir["#{File.expand_path('../test_helpers/**/*.rb', __FILE__)}"].each do |helper|
require helper
end

class Test::Unit::TestCase
include Assertions, TestHelper::Redis

class ActiveSupport::TestCase
include TestHelpers::Assertions
include TestHelpers::Redis

DatabaseCleaner.strategy = :truncation

def setup
Mocha::Mockery.instance.verify
Resque.redis = FakeRedis::Redis.new

Travis.pusher = TestHelpers::Mocks::Pusher.new
Resque.redis = FakeRedis::Redis.new

DatabaseCleaner.start

super
end
end

# class TestMochaTest < ActiveSupport::TestCase
# def test_mocha_expectation
# object = Object.new
# object.expects(:foo)
# end
# end
def teardown
Travis.pusher = nil

DatabaseCleaner.clean

super
end
end
22 changes: 0 additions & 22 deletions test/test_helper_rails.rb

This file was deleted.

84 changes: 43 additions & 41 deletions test/test_helpers/assertions.rb
@@ -1,53 +1,55 @@
module Assertions
def assert_flunks(message)
_wrap_assertion do
exception = nil

begin
yield
rescue Exception => e
exception = e
end
module TestHelpers
module Assertions
def assert_flunks(message)
_wrap_assertion do
exception = nil

begin
yield
rescue Exception => e
exception = e
end

if exception.nil?
flunk("expected <Test::Unit::AssertionFailedError #{message}> to be raised but nothing was raised.")
elsif !exception.is_a?(Test::Unit::AssertionFailedError) || (message != exception.message)
flunk("expected #<Test::Unit::AssertionFailedError: #{message.inspect}> to be raised but raised #<#{exception.class}: #{exception.message.inspect}>.")
if exception.nil?
flunk("expected <Test::Unit::AssertionFailedError #{message}> to be raised but nothing was raised.")
elsif !exception.is_a?(Test::Unit::AssertionFailedError) || (message != exception.message)
flunk("expected #<Test::Unit::AssertionFailedError: #{message.inspect}> to be raised but raised #<#{exception.class}: #{exception.message.inspect}>.")
end
end
end
end

def assert_equal_hashes(expected, actual)
diff = lambda do |lft_hash, rgt_hash, stack|
result = lft_hash.inject([]) do |result, (key, lft)|
stack << key
rgt = rgt_hash[key] rescue nil
if lft.is_a?(Hash) && rgt.is_a?(Hash)
result += diff.call(lft, rgt, stack)
elsif lft != rgt
result << [lft, rgt, stack.dup]
def assert_equal_hashes(expected, actual)
diff = lambda do |lft_hash, rgt_hash, stack|
result = lft_hash.inject([]) do |result, (key, lft)|
stack << key
rgt = rgt_hash[key] rescue nil
if lft.is_a?(Hash) && rgt.is_a?(Hash)
result += diff.call(lft, rgt, stack)
elsif lft != rgt
result << [lft, rgt, stack.dup]
end
stack.pop
result
end
stack.pop
result
end
result
end

format = lambda do |data|
lft, rgt, stack = *data
"Expected #{stack.inject(['actual']) { |result, key| result << "[#{key.inspect}]" }} to be: #{lft.inspect} but was: #{rgt.inspect}" if stack
end
revert = lambda do |data|
lft, rgt, stack = *data
[rgt, lft, stack]
end
format = lambda do |data|
lft, rgt, stack = *data
"Expected #{stack.inject(['actual']) { |result, key| result << "[#{key.inspect}]" }} to be: #{lft.inspect} but was: #{rgt.inspect}" if stack
end
revert = lambda do |data|
lft, rgt, stack = *data
[rgt, lft, stack]
end

messages = diff.call(expected, actual, []).map(&format)
messages += diff.call(actual, expected, []).map(&revert).map(&format)
messages = messages.uniq.compact
messages = diff.call(expected, actual, []).map(&format)
messages += diff.call(actual, expected, []).map(&revert).map(&format)
messages = messages.uniq.compact

defined?(MiniTest) ? self._assertions += 1 : add_assertion
flunk(messages.join("\n")) unless messages.empty?
defined?(MiniTest) ? self._assertions += 1 : add_assertion
flunk(messages.join("\n")) unless messages.empty?
end
end
end

Expand All @@ -56,7 +58,7 @@ def assert_equal_hashes(expected, actual)
require 'assertions/assert_flunks'

class AssertEqualHashTest < Test::Unit::TestCase
include Assertions
include TestHelpers::Assertions

def test_identical_hashes
expected = { 1 => 2 }
Expand Down
16 changes: 0 additions & 16 deletions test/test_helpers/buildable_helper.rb

This file was deleted.

34 changes: 18 additions & 16 deletions test/test_helpers/github_api.rb
@@ -1,21 +1,23 @@
require 'webmock/test_unit'

module GithubApiTestHelper
PATHS = %w(
api/v2/json/repos/show/svenfuchs/gem-release
api/v2/json/repos/show/travis-ci/travis-ci
api/v2/json/user/show/svenfuchs
api/v2/json/organizations/travis-ci/public_members
api/v2/json/user/show/LTe
)
module TestHelpers
module GithubApiTestHelper
PATHS = %w(
api/v2/json/repos/show/svenfuchs/gem-release
api/v2/json/repos/show/travis-ci/travis-ci
api/v2/json/user/show/svenfuchs
api/v2/json/organizations/travis-ci/public_members
api/v2/json/user/show/LTe
)

def setup
super
PATHS.each do |path|
filename = File.expand_path("../../fixtures/github/#{path}.json", __FILE__)
`curl -so #{filename} --create-dirs http://github.com/#{path}` unless File.exists?(filename)
stub_request(:get, "http://github.com/#{path}").
to_return(:status => 200, :body => File.read(filename), :headers => {})
def setup
super
PATHS.each do |path|
filename = File.expand_path("../../fixtures/github/#{path}.json", __FILE__)
`curl -so #{filename} --create-dirs http://github.com/#{path}` unless File.exists?(filename)
stub_request(:get, "http://github.com/#{path}").
to_return(:status => 200, :body => File.read(filename), :headers => {})
end
end
end
end
end

0 comments on commit 3ca2a8c

Please sign in to comment.