Skip to content

Instantly share code, notes, and snippets.

@aeden
Created May 8, 2012 13:26
Show Gist options
  • Save aeden/2634973 to your computer and use it in GitHub Desktop.
Save aeden/2634973 to your computer and use it in GitHub Desktop.
Set up for Rails and non-Rails specs playing nicely

You can then execute all specs with:

bundle exec rspec spec

And only run your decoupled tests with:

bundle exec rspec spec --tag ~rails
require 'spec_helper'
# Note that a symbol is used because the class will not be loaded when run in non-rails mode
# but the spec will still be loaded (but not executed)
describe :SomeClass :rails do
# this spec needs Rails
end
require 'spec_helper'
require 'some_other_class'
# You should require other dependencies here. This also has the benefit of exposing
# your dependencies - think about them.
describe SomeOtherClass do
# this spec does not need Rails
end
Rspec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
if config.filter_manager.exclusions.include?(:rails)
# Rails not wanted here
config.filter_run_excluding :rails => true
# since Rails isn't loaded, you need to include the paths that matter to you for requires
$:.unshift('./app')
# more setup for non-Rails tests
else
# Rails wanted here
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
end
# The following will allow you to use a symbol and still have subject work correctly.
config.shared_context :rails => :true do
subject do
Kernel.const_get(example.metadata[:example_group][:description_args].first).new
end
end
end
@Spaceghost
Copy link

I can't edit or amend my previous comment.

Here's my (spec/)[https://github.com/Velocitous/blog/tree/master/spec] directory. Mayhaps give me some feedback on the helpers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment