I have been developing gem.
For test my gem I use ActiveRecord.
When I run test I get some error because my gem doesn't support version 5.0 of ActiveRecord.
But in my gemspec I have:
spec.add_development_dependency 'activerecord', '~> 4.2'
Also I tried to user Gemfile.local but it didn't get positive result.
What a right way for set particular version for a gem?
I figure out problem was in my test_helper file:
begin
require File.dirname(__FILE__) + '/../../../../config/environment'
rescue LoadError
require 'rubygems'
gem 'activerecord'
gem 'actionpack'
require 'active_record'
end
When I setted explicit version for gem everything has worked fine:
gem 'activerecord', "4.2.7.1"
gem 'actionpack', "4.2.7.1"
A lot of the guides I've been finding don't use bundler.
this is the part of the gemfile I'm using for tests:
group :test do
gem "cucumber"
gem "cucumber-rails"
gem "launchy"
gem "hpricot"
gem "gherkin"
gem "capybara"
gem "rspec"
gem "rack"
gem "rspec-rails"
gem "webrat"
gem "database_cleaner"
gem "factory_girl"
gem "shoulda", :require => nil
gem "shoulda-matchers", :git => "https://github.com/thoughtbot/shoulda-matchers"
gem "cobravsmongoose"
gem "rcov"
gem "ZenTest"
gem "autotest-growl"
gem "inherited_resources", "1.0.2"
gem "responders", "0.4.2"
end
But even with all that, the generators never exist.
so doing: script/generate rspec
doesn't work, (can't find the rspec) generator
generators would be installed if the gems were installed as plugins... but I think that just adds bloat to the app, and different gems compile differently on different OSes.
So, anyone have any guides for setting up rspec with bundler with rails 2.3.x?
Setting up RSpec, Guard, and Spork on a Rails 2 project
I've done this a few times now; hopefully this will be helpful to anyone needing to maintain Rails 2.3 apps. This has worked great for the apps I've worked on, but I welcome contributions from others who suggest additional steps.
This guide assumes a Rails 2.3.x project on Bundler
Get rid of any old rspec plugins that are in your project, if any. RSpec bits may be hiding in:
Rakefile
lib/tasks/rspec.rake
vendor/plugins/rspec
(anything else you can find)
RSpec 2 is not compatible with Rails 2; use RSpec 1 (docs). Put the most recent compatible gem versions to your Gemfile:
group :test, :development do
gem 'test-unit', '1.2.3', :require => false # for rspec
gem 'rspec', '~> 1.2', :require => false
gem 'rspec-rails', '~> 1.2', :require => false
gem 'guard', :require => false
gem 'spork', '~> 0.8.0', :require => false
gem 'guard-rspec', :require => false
gem 'guard-spork', :require => false
gem 'growl', :require => false # notifications; optional
gem 'rb-fsevent', :require => false # for OSX; optional
gem 'listen', '>= 0.5.1', :require => false
gem 'machinist', '~> 2.0', :require => false
gem 'database_cleaner', '~> 0.9.1', :require => false
end
The :require => false options are optional, but it helps the app to start up faster in development if it doesn't have to load testing libraries outside of when SpecHelper.rb requires them.
Install the bundle. Use bundle update for any gems that were already in your Gemfile.
Ensure lib/tasks/rspec.rake and spec/spec_helper.rb do not exist.
script/generate rspec
Remove the config.gem line that was added to config/environments/test.rb; the app uses bundler.
spork --bootstrap
Then edit spec/spec_helper.rb and follow the instructions.
Move everything from the stock spec_helper.rb into the prefork block, except:
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
belongs in each_run.
Install database_cleaner. In spec/spec_helper.rb,
In the prefork block:
require 'database_cleaner'
In the each_run block:
DatabaseCleaner.clean
Initialize Guardfile
guard init spork
guard init rspec
Modify the Guardfile's rspec guard to use the correct version and drb (spork):
guard 'rspec', :version => 1, :cli => '--drb --color' do
Modify the Guardfile as appropriate for your project
Run rake spec. You should get no output (if you have no tests). If you get errors, resolve them.
Run guard. No errors? Great, test away!
Problems? Try again more quickly by running spec spec instead of re-running guard.
We still have an app on rails 2.3.8, but we updated it to use bundler (Gemfile), and it has rspec and cucumber working as well.
Make sure you follow the bundler guide to make your app correctly use the Gemfile's gem loading instead of Rails' default: http://gembundler.com/rails23.html
After you get that preinitializer.rb and change the config/boot.rb working correctly, you might need to make sure you're using the right versions of rspec and cucumber.
I think just that generic gem 'rspec-rails' might try installing rspec 2 for you, but that only works on Rails 3 (I believe), so you might need to specifically tell it to use rspec 1.x.
Our test group looks like this (although I think some of these gems may be older than they need to be, it's been awhile since we've updated them since a rails 3 upgrade for the app is pending we're not too worried about what it looks like right now):
group :test, :cucumber do
gem 'autotest-fsevent'
gem 'test-unit', '~>1.2.3'
gem "hoe", "1.5.1"
gem 'autotest-rails', '4.1.0'
gem 'rspec', '1.3.2'
gem 'rspec-rails', '1.3.4'
gem 'cucumber', '0.10.0'#, '0.9.0'
# Change this shinanigans to 0.4.0 when it gets released ;)
gem 'cucumber-rails', '0.3.2'
gem 'database_cleaner', '0.5.2'
gem 'capybara', '0.3.9'
gem 'launchy'
gem 'dupe', '0.5.1'
gem 'factory_girl', '1.2.4'
gem 'email_spec', '~>0.6.2', :require => false
end
After doing this, and running bundle install, I am able to type the command script/generate --help which includes this in the output:
Installed Generators
Rubygems: business_time_config, cucumber, culerity, dupe, email_spec, feature, integration_spec, paperclip, rspec, rspec_controller, rspec_model, rspec_scaffold
Builtin: controller, helper, integration_test, mailer, metal, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration
As you can see, the cucumber and rspec generators are in fact available there.
I think your problem might be the version of rspec it's installing. If it's installing rspec version 2, then that is tied to rails 3, which handles generators in gems differently I believe (I believe they have to be put in a different directory structure). That could be why your rails 2.3.x app isn't seeing them.
You don't have to follow my versions exactly, I'm not a fan (at all) of putting specific versions in a Gemfile but we ended up doing it here way back when because a) we didn't fully understand bundler, and b) We needed to make sure we were getting rails 2.3-compatible gems.
Hopefully this helps! Let me know if you have questions.
The reason the generators don't exist is that when you run rails generate ..., it's executing in the development environment while these gems are only loaded in the test environment.
Option 1
Add them to both development and test environments.
Option 2
Run rails generate ... RAILS_ENV=test
(I'm not positive that this option will work.)
In rails 5.1.4 you there are four easy steps to get your RSpec up and running:
group :development, :test do
gem "database_cleaner"
gem "rspec-rails"
end
Add the above gems to the :test and :development groups in your Gemfile.
run bundle install from the command line
run rails generate rspec:install from the command line, it will create the following files:
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
configure spec_helper.rb and rails_helper.rb
You can check more detailed info on: https://kolosek.com/rails-rspec-setup.
I'm trying to use mocha in my Rails 3 project but keep getting the following exception:
NoMethodError: undefined method `mock' for #<MochaTest:0x00000101f434e8>
/Users/John/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.10/lib/action_dispatch/testing/assertions/routing.rb:175:in `method_missing'
test/functional/mocha_test.rb:7:in `block in <class:MochaTest>'
Test
I've written the simplest test possible:
require 'test_helper'
class MochaTest < ActionController::TestCase
test "test mocha" do
order = mock('order')
end
end
I run it using ruby -Itest test/functional/mocha_test.rb
I've tried rake test and it gives exactly the same exception.
test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
#....
require 'test/unit'
require 'mocha'
end
GemFile
source 'http://rubygems.org'
gem 'rails', '3.0.10'
gem 'sqlite3'
gem 'devise', '1.4.5'
gem 'activemerchant'
gem 'geo_location'
gem 'nokogiri'
gem "nifty-generators", :group => :development
gem 'mocha', '0.10.0'
Things I've Tried
Installing mocha 0.9.5 after reading 0.9.5-7 had issues with this. I get an undefined method name exception instead
Changing where I require mocha - at the bottom of test_helper.rb, top of test etc.
Tried calling mock() in rails console test - I get the same exception
I'm tearing my hair out with this. Any ideas would be gratefully received.
I found the answer immediately after I'd posted this.
Here's the answer: Mocha Mock Carries To Another Test
In short, I changed
gem 'mocha', '0.10.0'
to
gem 'mocha', '0.10.0', :require => false
and it worked like a charm!
So, I recently moved to bundler, and I'm having issues getting everything working again.
when I run bundle exec rake test:units, I get this error:
....../gems/factory_girl-1.2.4/lib/factory_girl/factory.rb:327:in `factory_by_name': No such factory: user (ArgumentError)
and, I saw in a another stack overflow post that someone fixed the problem by adding
FactoryGirl.find_definitions
but, that errors in saying that I have duplicate definitions (which I don't, cause search)
I even tried having only one factories file. but the error continued.
top of test_helper:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
require "bundler/setup"
Bundler.require(:test)
test group in gem file
group :test do
gem "cucumber", "~>0.10.3"
gem "cucumber-rails", "0.3.2"
gem "launchy"
gem "hpricot"
gem "gherkin", "~>2.4.0"
gem "capybara", "0.4.1.2"
gem "rspec", "1.3.2"
gem "rspec-rails", "1.3.2"
gem "rspec-core"
gem "rspec-expectations"
gem "webrat", "0.7.0"
gem "database_cleaner"
gem "factory_girl", "1.2.4"
gem "shoulda"
gem "awesome_print"
gem "cobravsmongoose"
end
I've upgraded my app from using config.gem to a Gemfile with bundler and have noticed that my unit tests have now stopped running. It's a bit strange and I'm not entirely sure where to start looking.
When I run rake test:units --trace I can see my environment being setup and it lists the files it intends to execute, but then it just returns.
It does the same thing if I try to run one individual file using something like: rake -I"lib:test" test/unit/foo.rb or using autotest.
It's all very strange. It's as if the files are being loaded but the actual unit tests are not being run.
I'm using shoulda and fast_context and I thought these might be the problem but if I include a unit test using the standard def test_ syntax it still doesn't get run so I don't think it's those.
Any hints or pointers would be greatly appreciated. I feel like I'm coding blind until I can get them working again!
So here's where I am now:
My reasons for using bundler are for installing dependencies on heroku and because I wanted to use a gem sourced from a git repo on github. The long and the short of it is that I've removed the preinitializer for bundler and have gone back to using config.gem. To get around the fact that I can't use a github repo using config.gem I've pushed out my own copy to rubygems. Was this the right move?
Here's the preinitializer.rb
begin
require "rubygems"
require "bundler"
rescue LoadError
raise "Could not load the bundler gem. Install it with `gem install bundler`."
end
if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.24")
raise RuntimeError, "Your bundler version is too old for Rails 2.3." +
"Run `gem install bundler` to upgrade."
end
begin
# Set up load paths for all bundled gems
ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
Bundler.setup
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." +
"Did you run `bundle install`?"
end
I don't know how the .gems file would be useful because it's a heroku only thing and I'd have to hunt through git for it, but here's my gemfile.
source :gemcutter
gem 'rails', '2.3.9'
gem 'pg'
gem 'minitest'
gem 'RedCloth'
gem 'erubis'
#gem 'memcached'
gem 'daemons'
gem 'resque'
gem 'inherited_resources', '1.0.6'
gem 'clearance', '0.8.8'
gem 'acl9'
gem 'sprockets'
gem 'aws-s3'
gem 'paperclip', '2.3.1.1'
gem 'rmagick', '2.12.2'
gem 'jonnii-cheddargetter', '0.1.3'
gem 'attribute_normalizer'
gem 'formtastic', '1.1.0.beta'
gem 'will_paginate', '2.3.14'
gem 'hoptoad_notifier'
gem 'mixpanel_client'
gem 'sunspot'
gem 'websolr-sunspot_rails'
gem 'geokit'
gem 'ri_cal'
gem 'jonnii-yelp'
group :development, :test do
gem 'test-spec'
gem 'shoulda'
gem 'redgreen'
gem 'factory_girl'
gem 'populator'
gem 'faker'
gem 'ZenTest'
gem 'autotest-rails'
gem 'webrat'
gem 'cucumber'
gem 'cucumber-rails'
gem 'database_cleaner'
gem 'parallel'
gem 'hydra'
gem 'heroku'
gem 'taps'
gem 'ruby-prof'
gem 'treetop'
gem 'rspec'
gem 'rspec-rails'
end
Got the same problem.Just remove the gem 'hydra' will get the unit test back to normal
Do you have this at the end of your config/boot.rb file:
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
#bundler_loaded ||= Bundler.require :default, Rails.env
end
end
Rails::Initializer.run(:set_load_path)
end
end
(from http://gembundler.com/rails23.html)
I recently had trouble running specs for a project. The reason was that I was missing a line from config/application.rb. Nowadays that line pops in by default when you create a new rails 3 project, but if your project has been initialized some time ago it might be missing.
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)