Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests tasks in Capfile #5

Open
crizCraig opened this issue Mar 1, 2013 · 0 comments
Open

Tests tasks in Capfile #5

crizCraig opened this issue Mar 1, 2013 · 0 comments

Comments

@crizCraig
Copy link

Thought this could help someone:

require 'rubygems'
gem 'minitest'

require 'minitest/autorun'
require 'capistrano'

require 'minitest-capistrano'
require 'capistrano/cli'

describe 'my_project' do

  before do
    ENV['TEST'] = 'true'
    @config = Capistrano::Configuration.new
    @config.extend(MiniTest::Capistrano::ConfigurationExtension)
    @config.load file: File.expand_path(File.join(File.dirname(__FILE__), 'Capfile'))
  end

  subject { MiniTest::Unit::TestCase.new 'the test case' }

  it 'does something in production when the task role is production' do
    test_environment 'do_something_on_production', 'production'
  end

  it 'does something in staging when the task role is staging' do
    test_environment 'do_something_on_staging', 'staging'
  end

  it 'does something in staging when the task role is not defined' do
    test_environment 'do_something', 'staging'
  end
end

def test_environment(task, environment)
  subject.assert_have_task task.to_sym, @config
  @config.find_and_execute_task task
  subject.assert_have_run "echo #{environment}", @config
end

Some gems may not load in the test environment, hence the ENV['test'] and

def this_is_a_test
  ENV['TEST'] == 'true'
end

unless this_is_a_test
  require 'hipchat/capistrano'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant