Getting started with rspec - ruby-on-rails

I'm trying to do the labs on TestFirst.org with rspec. I've installed rspec and initilized it in the project directory. But when i "rake" i get this error message:
Could not find 'rspec' <~> 2) - did find: [rspec-3.0.0]
C:/Sites/RubyTest/RubyTesting/learn_ruby/rakefile:2:in `<top (required)>ยด
My versions:
ruby 1.9.3p545
rails 4.1.1
rspec 3.0.2
Seems like I've got the wrong version of rspec or something. My OS is windows 7 btw.
This is the content of rakefile:
gem 'rspec', '~>2'
require 'rspec/core/rake_task'
task :default => :spec
desc "run tests for this lab"
RSpec::Core::RakeTask.new do |task|
lab = Rake.application.original_dir
task.pattern = "#{lab}/*_spec.rb"
task.rspec_opts = [ "-I#{lab}", "-I#{lab}/solution", '-f documentation', '-r ./rspec_config']
task.verbose = false
end

This is because rspec ~3.0.0 is already activated and your rake file requires 'rspec', '~>2'. Change rspec version in Rakefile to fix this.
gem 'rspec', '~>3'

Looks like you have a installed version of rspec (3.0.2) that is more recent than the one expected by the project (~ 2.x).
One simple way to solve it would be to force bundle to use the project version:
bundle exec rake
(instead of just using rake)

Related

Minitest plugins (ie. minitest/pride) not working after installing rails

I have two Ruby projects (1 Rails, 1 vanilla). Both using Ruby 3.0.3. In my Ruby project (before bundling my Rails one), Minitest::Pride is loaded and works when running rake test. But, after bundling the Rails project, I'm back to old red/green colors for rake test in the Ruby project.
Here's the file structure (for simplicity's sake, I'm stripping the Rails project down to just a Gemfile, which I can reproduce this error with on a clean Ruby Rbenv install):
ruby-project/
-- test/
-- example_test.rb
-- test_helper.rb
Gemfile
Rakefile
rails-project/
-- Gemfile
ruby-project/test/example_test.rb:
require "test_helper"
class ExampleTest < Minitest::Test
25.times do |i|
define_method "test_success#{i}" do
assert true
end
end
end
ruby-project/test/test_helper:
require "minitest/autorun"
require "minitest/pride"
ruby-project/Gemfile:
source "https://rubygems.org"
gem "rake"
gem "minitest"
ruby-project/Rakefile:
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs = %w(lib test)
t.pattern = "test/**/*_test.rb"
end
task :default => :test
rails-project/Gemfile:
source "https://rubygems.org"
ruby "3.0.3"
gem "rails", "~> 7.0.3"
Steps to Reproduce (with rbenv already installed):
Build two projects like the ones above
rbenv install 3.0.3
From ruby-project dir: bundle
From ruby-project dir: rake test (Minitest::Pride plugin loaded)
From rails-project dir: bundle
From ruby-project dir: rake test (Minitest::Pride plugin NOT loaded)
Things I have tried:
locking Minitest to the exact same version for both projects (5.16.3 and 5.17.0)
locking Rails to 7.0.3.1 and 7.0.4.2
Bundling the Rails project first
I'm running on a M1 Mac, but I'm not sure this matters. Interestingly, rails test in a real Rails project WILL load Minitest::Pride.
Any ideas on what's going on and how to fix?

Why does Ruby run only some tests and not others?

I'm using this command to run some tests...
bundle exec ruby -Itest test/functional/*.rb
In my test/functional dir I have two files...
file_sets_controller_test.rb
user_sessions_controller_test.rb
With the above command, the tests in file_sets_controller_test.rb all run but the ones in user_sessions_controller_test.rb don't run at all -- no errors or other output is reported.
However, I can run that file directly no problem, with this...
bundle exec ruby -Itest test/functional/user_sessions_controller_test.rb
That works fine.
I know that another option is to use rake test functionals, but that is extremely slow compared to running them directly.
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2]
Rails 3.2.12
Here's a part of my Gemfile...
group :development, :test do
gem 'ansi'
gem 'turn'
gem 'minitest'
gem 'minitest-matchers'
end
And here's my test_helper.rb...
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'turn/autorun'
Turn.config.ansi = true
require 'minitest/autorun'
class ActiveSupport::TestCase
fixtures :all
end
Removing the Turn and Minitest gems doesn't change anything as far as I can tell.
The ruby command takes a ruby file to run as its first argument and makes additional arguments available to the ruby program. The shell is expanding your glob expression into 2 arguments and passing them to ruby, so ruby is running the first file name in the expansion.
Additional:
I think you can do what you want with something like...
bundle exec ruby -Itest -e "Dir.glob('test/functional/*_test.rb').each{|f| require File.expand_path(f)}"

ruby on rails rspec error

I'm following through the "Learn Rails by Example" book, and I'm trying to run the tests. For some reason I can't get rspec to work properly.
If I run the rspec spec/ command as he instructs, I get the following error:
$ rspec spec/
/home/desktop/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.21/lib/bundler/runtime.rb:31:in `block in setup':
You have already activated rspec-core 2.7.1, but your Gemfile requires rspec-core 2.6.4.
Using bundle exec may solve this. (Gem::LoadError)
The odd thing is my Gemfile doesn't specify version--
group :development do
gem 'rspec-rails'
end
group :test do
gem 'rspec'
gem 'webrat'
end
If I follow the advice from the error message and use bundle exec rspec spec/ then the first two tests pass-- but the new "about" page we built in the tutorial fails with the following error, even though as far as I can tell the page I'd built (and controller actions etc.) are exactly as they should be:
Failures:
1) PagesController GET 'about' should be successful
Failure/Error: response.should_be_success
NoMethodError:
undefined method `should_be_success' for #<ActionController::TestResponse:0x00000003539438>
# ./spec/controllers/pages_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
Finished in 0.10861 seconds
3 examples, 1 failure
Failed examples:
rspec ./spec/controllers/pages_controller_spec.rb:21 # PagesController GET 'about' should be successful
I'm a pretty experienced programmer but I've run into endless issues with conflicting gem versions and a hundred different ways to accomplish all the different tasks using Rails (eg. "use RVM", "Don't use RVM", "install gems using sudo", "don't install gems using sudo" etc.)
My dev machine is running ubuntu linux.
Thanks for any help-- please explain if you would what I'm doing wrong in Ruby noob language!
Running bundle exec is correct, and is needed because you have a newer version of that gem installed that gets loaded instead of the one specified in your Gemfile.lock. Using bundle exec overrides the load path, causing only the gems specified in your Gemfile.lock to be loaded. (You may find it handy to alias bundle exec to something shorter.)
The answer to the second problem is right in the error messages:
undefined method `should_be_success'
it should be should be_success.

uninitialized constant ActiveMerchant::Validateable::HashWithIndifferentAccess when running ActiveMerchant's unit tests

I am trying to run the unit tests of the ActiveMerchant gem version
1.4.1: https://github.com/Shopify/active_merchant/tree/v1.4.1
First I ran: rake -T and got the error:
uninitialized constant ActiveMerchant::Validateable::HashWithIndifferentAccess
I fixed that first error by adding:
gem 'activesupport', "=2.3.4"
require 'active_support'
at the top of the Rakefile(https://github.com/Shopify/active_merchant/blob/v1.4.1/Rakefile). Note that I had to specify the exact version (I guess HashWithIndifferentAccess doesn't exist in later versions of ActiveSupport).
Now, if I run: rake test:units, I get the same error:
uninitialized constant ActiveMerchant::Validateable::HashWithIndifferentAccess
Here is the task that it tries to run (in the same Rakefile):
Rake::TestTask.new(:units) do |t|
t.pattern = 'test/unit/**/*_test.rb'
t.ruby_opts << '-rubygems'
t.verbose = true
end
How can I get rid of this error? Do I need to specify the activesupport gem inside the task?
gem 'activemerchant' # => Ruby 1.9.2 / AM 1.12.x
vs
gem 'active_merchant' # => ? / AM 1.5.2
ActiveMerchant is on version 1.12.0, so 1.4.1 is quite old - have you tried this with the latest version?

Rails shoulda and factory_girl setup

I have installed both shoulda and factory_girl, I can run shoulda just fine, but when I add this:
require 'factory_girl'
Factory.define :user do |u|
u.mail 'test#example.com'
u.pass 'secret'
end
to my test/test_helper.rb I'm getting this error:
/test/test_helper.rb:1:in `require': no such file to load -- factory_girl (LoadError)
when I execute rake test:units
I have installed both gems using:
sudo gem install thoughtbot-shoulda --source=http://gems.github.com
sudo gem install thoughtbot-factory_girl --source=http://gems.github.com
and can see both of them being installed fine.
And by the way, this works fine as well:
script/console
Loading development environment (Rails 2.3.8)
require 'factory_girl'
=> []
so requiring the gems seems to be working
Couple things to check:
Don't use --source=http://gems.github.com anymore. This has been deprecated in favor for gemcutter.org
Don't use "thoughtbot-gem_name" - again this was deprecated by gemcutter.org. Just do sudo gem install factory_girl shoulda
Make sure you have config.gem 'factory_girl' in your config/environments/test.rb file.
if you are using spork, make sure to restart it. Took me a few moments to realize that.

Resources