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
@gstark
Copy link

gstark commented May 8, 2012

So do you now make two rspec passes? (with and w/o Rails dependency)

@aeden
Copy link
Author

aeden commented May 8, 2012

Ideally I want to move all specs to be decoupled whenever possible. I would then run those specs in VIM as I'm working on a class. I consider specs coupled with Rails to be integration tests and ultimately I want those to be run by my CI. I'm not there yet, but that's what I'm evolving the code towards. At the moment I typically just run the specs with Rails enabled which will run both the decoupled and non-decoupled tests.

@Spaceghost
Copy link

Here's what I've come up with so far in terms of keeping tests in line.

Reorganize spec helpers

I'm not done yet, it evolves as my project does. I'm using it in a couple places, but I haven't used any meaningful tags yet.
I have some ideas for some.

@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