Re: [rspec] Rspec 2/Mongoid 4/Rails 4/ spec_helper config

892 views
Skip to first unread message

Aaron Kromer

unread,
Jun 11, 2013, 8:55:18 AM6/11/13
to rs...@googlegroups.com

I have a working setup which looks very similar to yours. I've highlighted the main differences below:

Gemfile

ruby '2.0.0'

gem 'rails', '4.0.0.rc1'

gem "mongoid", github: 'mongoid'

group :development, :test do
  gem 'rspec', '~> 2.14.0.rc1'
  gem 'rspec-rails', github: 'rspec/rspec-rails'
  gem 'mongoid-rspec'

end

config/application.rb

Bundler.require(:default, Rails.env)

spec/spec_helper.rb

RSpec.configure do |config|
  config.before do
    Mongoid.purge!
  end
end


On Mon, Jun 10, 2013 at 11:12 PM, dbkbali <dbk...@gmail.com> wrote:
I have a working app with Mongoid 4 (Master Branch) and Rails 4 and was trying to switch my app over from using minitest to rspec. Using minitest I am having no problems getting my tests running and passing with my Test Helper file. However when setting up a spec_helper file for RSPEC it appears that my that Mongoid is not getting loaded at the right time as my ./bin/rspec craps out in application.rb file when the "Bundler.require(*Rails.groups(assets: %w(development test)))" is executed, and it encounters the first gem in my gem file that uses mongoid.

require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
#require "active_resource/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
Bundler.require(*Rails.groups(assets: %w(development test)))
#if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  #Bundler.require(*Rails.groups(assets: %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  #Bundler.require(:default, :assets, Rails.env)
#end


module MyApp
......


#app/spec/spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'database_cleaner'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
#ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  #config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  #config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"


  config.before(:each) do
    db = Mongoid::Config::master
    # ignore stuff like system.indexes
    db.collection_names.reject {|c| c =~ /^system/}.each {|c| db.drop_collection c}
  end


  config.include Capybara::DSL
end


Have searched the docs and tried various permutations of requiring  "mongoid" and Mongoid.load! but am not having any luck getting this working so that rspec has access to the Mongoid database in the test environment.

If anyone has a working config for  Rspec 2/Rails 4/Mongoid 4 it would be much appreciated if you can help me out or point me in the right direction to get this working.

Many Thanks,

David Krett

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/78443e65-0336-4bd5-aafa-2188885f28ef%40googlegroups.com?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Aaron Kromer

unread,
Jun 12, 2013, 11:20:06 AM6/12/13
to rs...@googlegroups.com

Did you change your bundler statement in your application.rb? I noticed ours were slightly different. I doesn't seem like you're loading the default gems.



On Wed, Jun 12, 2013 at 2:05 AM, dbkbali <dbk...@gmail.com> wrote:
Thanks Aaron,

I tried your config, but am still having problems, it seems for some reason that as soon as the Bundler.require is executed in application.rb and it hits a gem that needs a Mongoid Class, that it errors out for example I am using mongoid_slug and I get the following messages.

/Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/mongoid_slug-3.1.1/lib/mongoid/slug/criteria.rb:5:in `<module:Slug>': uninitialized constant Mongoid::Criteria (NameError)
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/mongoid_slug-3.1.1/lib/mongoid/slug/criteria.rb:4:in `<module:Mongoid>'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/mongoid_slug-3.1.1/lib/mongoid/slug/criteria.rb:3:in `<top (required)>'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/mongoid_slug-3.1.1/lib/mongoid_slug.rb:4:in `require'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/mongoid_slug-3.1.1/lib/mongoid_slug.rb:4:in `<top (required)>'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `require'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `each'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `block in require'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `each'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `require'
from /Users/dbk/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler.rb:132:in `require'
from /Users/dbk/Development/w2d/config/application.rb:10:in `<top (required)>'
from /Users/dbk/Development/w2d/config/environment.rb:3:in `require'
from /Users/dbk/Development/w2d/config/environment.rb:3:in `<top (required)>'
from /Users/dbk/Development/w2d/spec/spec_helper.rb:4:in `require'
from /Users/dbk/Development/w2d/spec/spec_helper.rb:4:in `<top (required)>'
from /Users/dbk/Development/w2d/spec/request/home_spec.rb:1:in `require'

So for some reason my configuration is not loading Mongoid in the test environment. The strange thing is that I do not have this problem with minitest.

Again thanks for sharing.

dbkbali

unread,
Jun 13, 2013, 1:14:32 AM6/13/13
to rs...@googlegroups.com
I did have the bundler change incorporated. Making progress, though I believe it has something to do with rbenv, binstubs or bundler, if I install rspec outside my bundle and issue an rspec command - My tests run ok, however if I run bundle exec rspec or create a binstub - I get the same error. So right now just trying to understand binstubs and bundler better to determine why I am getting this problem.

If you or anyone has any suggestions based on the above info, it would be much appreciated

Again Thanks,

David

Weston Platter

unread,
Jul 20, 2013, 7:17:54 AM7/20/13
to rs...@googlegroups.com
Aaron - thanks for the code snippets. I used them to setup an more recent version. Thought I'd post in case it's helpful for others.

ruby '2.0.0'
gem 'rails', '~> 4.0.0'
gem 'mongoid', github: 'mongoid/mongoid'

group :development, :test do
  gem 'rspec', '~> 2.14.0'
  gem 'rspec-rails', '~> 2.14.0', github: 'rspec/rspec-rails', branch: '2-14-maintenance'
  gem 'mongoid-rspec'
end

end

Brandon Croft

unread,
Jul 31, 2013, 4:46:41 PM7/31/13
to rs...@googlegroups.com
David, did you make any progress on this issue? I'm converting a project from minitest to rspec and have the same problem.


On Wednesday, June 12, 2013 11:14:32 PM UTC-6, dbkbali wrote:

Brandon Croft

unread,
Aug 16, 2013, 1:23:24 PM8/16/13
to rs...@googlegroups.com
I discovered that i had a "mongoid.rb" file in my lib folder, which was being loaded in place of the mongoid gem.

Not sure why only rspec was affected by this.
Reply all
Reply to author
Forward
0 new messages