I'm following the NetTuts intro to Rails screencast to get a better handle on rspec/capybara/guard etc. and after a few errors I cannot get passed this one:
/home/jonlee/.rvm/gems/ruby-2.1.1#railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/rails.rb:15:in `<top (required)>': undefined method `join' for nil:NilClass (NoMethodError)
from /home/jonlee/Projects/rails/guardtest/spec/spec_helper.rb:3:in `require'
from /home/jonlee/Projects/rails/guardtest/spec/spec_helper.rb:3:in `<top (required)>'
My spec_helper is as follows:
require 'rails'
require 'rspec/core'
require 'capybara/rails'
RSpec.configure do |config|
config.include Capybara::DSL
end
my gemfile has:
group :test, :development do
gem 'rspec-core'
gem 'capybara'
gem 'guard-rspec'
end
im using:
Ruby - 2.1.1
Rails - 4.0.5
rspec - 3.0.1
capybara - 2.3.0
Even after suggestion of changing spec_helper.rb file to require 'rspec/core' and changing gem to rspec-core I still have this error.
Does the order in the spec_helper matter or do I need to perform some further work in the Rspec.configure block?
GIT - https://github.com/JonleePeakman/guardtest
Your gemfile has gem 'rspec-core'. It should be gem 'rspec-rails'.
You have RSpec 3.0.1 and there are big changes in configuration compared to previous versions. Be careful not to follow outdated tutorials or blog posts. Have you used the RSpec generator to set up RSpec?
$ rails generate rspec:install
You should have files:
.rspec
spec/spec_helper.rb
spec/rails_helper.rb
Capybara will work "out of the box" without any changes to the configuration files. Try removing your spec/spec_helper.rb file and use the RSpec generator to set things up.
As an alternative to the NetTuts tutorial, you might want to look at the RSpec Tutorial I've written which is up to date with RSpec 3.0.
Related
I'm adding Rspec tests to a project that didn't have rspec before, or automated tests at all. It is running on a Windows 7 box. When I run rspec, it shows the error:
undefined method `has_attached_file'
The main issue is that I'm writing just a simple test for a model that is not using Paperclip at all. Paperclip is used on another model.
I've researched extensively, trying to include the gem in test environment but nothing seems to solve the issue.
Is there a way of skipping or forcing the inclussion of paperclip when I run the rspec tests?
My gemfile has, among other things, the following:
source 'http://rubygems.org'
ruby "1.9.2"
gem 'rails', '3.0.20'
...
gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git'
gem 'aws-s3'
gem 'aws-sdk'
group :test, :development do
gem 'rspec-rails', '~> 2.14.0'
end
group :test do
gem 'sqlite3'
gem 'capybara', '2.0'
gem 'selenium-webdriver'
gem 'shoulda-matchers'
gem 'database_cleaner'
gem 'launchy', '2.2.0'
gem 'factory_girl_rails'
end
I run the rspec command:
bundle exec rake spec
I get the following error:
C:/Users/MAC/.pik/rubies/Ruby-192-p290/bin/ruby.exe -S rspec ./spec/models/filter_category_spec.rb ./spec/models/filter_spec.rb
C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/attr_encrypted-1.2.1/lib/attr_encrypted.rb:241:in `method_missing': undefined method `has_a
ttached_file' for #<Class:0x5278770> (NoMethodError)
from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.0.20/lib/active_record/base.rb:1018:in `method_missing'
from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/attr_encrypted-1.2.1/lib/attr_encrypted/adapters/active_record.rb:50:in `metho
d_missing_with_attr_encrypted'
....
from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/railties-3.0.20/lib/rails/application.rb:77:in `method_missing'
from C:/Avity/DS Candidates Tracking System/ds-cts/config/environment.rb:5:in `<top (required)>'
from C:/Avity/DS Candidates Tracking System/ds-cts/spec/spec_helper.rb:3:in `require'
from C:/Avity/DS Candidates Tracking System/ds-cts/spec/spec_helper.rb:3:in `<top (required)>'
from C:/Avity/DS Candidates Tracking System/ds-cts/spec/models/filter_category_spec.rb:1:in `require'
My spec_helper file is:
# 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'
# 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}
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
#Database cleaner
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
#Use chrome driver
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.current_driver = :selenium_chrome
end
Any ideas of what should I do to make this work properly?
EDIT It seems the issue comes before loading spec_helper.rb
If I make changes on this file, like adding errors on purpose, nothing happens and the issue above keeps happening.
The only way I have to run the tests is by commenting the has_attached_file line on my model and adding the group option to the paperclip gem on my Gemfile, to avoid having it on test environment:
gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git', :group=>[:development,:production]
Not sure how to solve this without skipping Paperclip from test env.
The problem seems to me to be with shoulda-matchers and paperclip
Add to your spec_helper.rb file
require "paperclip/matchers"
below where you are
require "capybara/rspec"
And add
config.include Paperclip::Shoulda::Matchers
below
RSpec.configure do |config|
Check out the shoulda-matchers documentation for paperclip
Hope this helps
I ran into this issue when trying to use Paperclip in a gem that has ActiveRecord, but not Rails. I was able to fix this for tests by adding a spec/support/paperclip.rb file with the following:
require 'paperclip/railtie'
RSpec.configure do |config|
# Bind paperclip to enable ActiveRecord #has_attached_file method
Paperclip::Railtie.insert
end
As also suggested, this is a good place to add config.include Paperclip::Shoulda::Matchers.
But I'm using Rails 4.2 and Rspec 3, so I think the order of things loaded is much different than yours.
When running my rails 4 rspec suite via rake everything works correctly but when attempting to run rake simplecov I get hundreds of failures all w/ a NoMethodError something like:
1) Answer Validations
Failure/Error: it { should validate_presence_of :text }
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup::Nested_16::Nested_1:0x007faf9fb5b7c8>
# ./spec/models/answer_spec.rb:12:in `block (3 levels) in <top (required)>'
Any clues as to why this is happening? I should also mention that I'm testing using sqlite3 :memory:.
The shoulda libraries aren't being included in your simplecov run.
Is simplecov run in a different environment than :test perhaps? That may make the shoulda gem not be loaded if it's only in the :test group in your Gemfile.
How are you including simplecov in your spec_helper? Something like this?
require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.start 'rails' if ENV['COVERAGE']
As the very first lines of your spec_helper.rb?
Something like this in your Gemfile?
group :test, :development do
...
gem 'rspec-rails', '~> 2.0'
gem 'shoulda-matchers'
end
group :test do
gem 'simplecov'
gem 'simplecov-rcov'
end
And executing it like so?
$ COVERAGE=true bundle exec rake spec
This recipe is working very well for me in many projects. The RCov formatter may not be important depending on your purposes.
I have a Rails 3.2 application in which I use paperclip to upload and store file attachments. This works great in itself, but I want to test it using rspec.
Now, the documentation provides some pretty nifty shoulda matchers to do just that. However, when I try to run them, it says my configuration in the spec helper is wrong:
uninitialized constant Paperclip::Shoulda (NameError)
I have the following in my spec helper:
RSpec.configure do |config|
config.include Paperclip::Shoulda::Matchers
end
And this is in my Gemfile:
group :development, :test do
gem "rspec-rails", "~> 2.0"
gem "shoulda-matchers"
end
I am not sure what I am missing here?
I found out what the problem was. I had require 'paperclip/matchers' behind the shoulda matchers, but instead I first need to require the paper clip matchers, and only afterwards include the shoulda matchers.
UPDATE 2: require {path_to_spec_helper} solves the setup undefined issue, but now all of the static variables are suddenly undefined, and All FactoryGirl-made objects don't pass validation (even though inspecting shows that the object should pass validation). And Changing FactoryGirl to save_with_validation(false) just makes the object nil in my tests, breaking everything.
UPDATE:
I threw this into my code:
context "some context" do
ap self.respond_to?(:setup)
setup do
raise "stop"
And the respond_to line printed true, but then proceeded to throw the method_missing error below. So, I guess it's just undefined within context? It didn't used to be that way.
Original Post:
For some reason, unknown to me, it seems that context / should / setup are undefined in my tests. I'd change all the setup's to before(:each)'s and then there would be a problem with should or context. When I change all of the rspec / shoulda matchers to the old-skool style of describe - before(:each) - it{}, my tests will run, but won't actually get executed. (the progress in the console shows no tests being run (no dots)).
So, I guess, how do I verify my test environment is set up properly?
Here is my configuration
gem file:
# can't be in test group because it's required in the rake file
gem "single_test"# because running all the tests all the time takes too long
group :test do
# helper gems
gem "rspec-rails", "1.3.4"
gem "rspec", "1.3.2"
gem "shoulda"
gem "database_cleaner"
gem "crack" #for XML / JSON conversion
gem "mocha" #required for .requires and .stubs
gem "factory_girl", :require => false
# view and functional
gem "capybara", "1.1.1"
gem "cucumber", "1.1.0"
gem "cucumber-rails", "0.3.2"
gem "culerity"
gem "launchy"
gem "hpricot"
gem "gherkin"
gem "rack"
gem "webrat"
# tools
gem "json"
gem "curb"
end
Required things in test hepler:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require "bundler/setup"
Bundler.require(:test)
require 'test_help'
require 'spec'
require 'spec/expectations'
require 'factory_girl'
binary info:
ruby 1.8.7
rvm 1.7.2
gem 1.8.21
bundle 1.1.4
rake 0.9.2.2
rails 2.3.14
And my error:
`method_missing': undefined method `setup' for Spec::Example::ExampleGroup::Subclass_1:Class (NoMethodError)
stack trace:
from test/unit/my_test.rb:257
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_methods.rb:188:in `module_eval'
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_methods.rb:188:in `subclass'
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_methods.rb:55:in `describe'
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_factory.rb:31:in `create_example_group'
code around line 257 of the last code bit on the stack:
context "some context" do
setup do
...
If you didn't, first you have to generate your spec/spec_helper.rb and spec/rails_helper.rb files. you can do this with the following command:
rails generate rspec:install.
Once the files are created, make sure rails_helper.rb requires spec_helper.rb, otherwise it will not work and require "rails_helper" on top of your spec files.
You can check more detailed configuration of rails_helper.rb and spec_helper.rb here: https://kolosek.com/rails-rspec-setup
Michael Hartl's wonderful Rails Tutorial is now available for Rails 3.2. He continues to outline TDD and BDD using rspec and spork as he did in version 3.0 and adds guard in 3.2. In version 3.0, Hartl includes information about autotest and I was able to get the wonderful growl notifications working. In 3.2, however, he no longer includes autotest or much growl information. Guard is working nicely with spork but there's no notifications. I've ventured out on my own the last couple hours using Hartl's 3.0 and some blog posts but trying to get autotest to work still produces a "LoadError" and a growl notification "could not run tests." Super grateful for any thoughts. I'm on OS X 10.7.3. Here's what I did:
$ gem install autotest -v 4.4.6
$ gem install autotest-rails-pure -v 4.1.2
$ gem install autotest-fsevent -v 0.2.8
$ gem install autotest-growl -v 0.2.16
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'pg', '0.12.2'
group :development, :test do
gem 'rspec-rails', '2.9.0'
gem 'guard-rspec', '0.5.5'
end
group :assets do
gem 'sass-rails','3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test do
gem 'capybara', '1.1.2'
gem 'rb-fsevent', '0.4.3.1', :require => false
gem 'growl', '1.0.3'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
end
~/.autotest
require 'autotest/growl'
require 'autotest/fsevent'
Autotest::Growl::show_modified_files = true
Autotest::Growl::one_notification_per_run = true
Autotest::Growl::clear_terminal = false
Autotest::Growl::hide_label = true
Autotest.add_hook :initialize do |autotest|
autotest.add_mapping(/^spec\/requests\/.*_spec\.rb$/) do
autotest.files_matching(/^spec\/requests\/.*_spec\.rb$/)
end
end
$ autotest
loading autotest/rails
--------------------------------------------------------------------------------
/Users/[me]/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit spec/requests/static_pages_spec.rb].each { |f| require f }"
/Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- spec_helper (LoadError)
from /Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/[me]/programing/rails/rdale_house/spec/requests/static_pages_spec.rb:1:in `<top (required)>'
from /Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from -e:1:in `block in <main>'
from -e:1:in `each'
from -e:1:in `<main>'
spec/spec_helper.rb
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# 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'
# 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}
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
config.mock_with :rspec
# 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
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
I ended up just adapting to Hartl's workflow using Sublime Text 2 as well, however, I did want to make sure that I could switch over to Guard with Growl notifications if I ever wanted to (no need for autotest as far as I know since I believe that's Guard's job), so this is how I did it in my personal environment set up guide. Hope it can be of some assistance to you. I'm on OSX 10.6.8, so there may be some differences you need to do on 10.7.3:
Setup Growl for RSpec test notifications (For Mac):
Growl is likely already installed on the system, but growlnotify probably is not.
Download the Growl-1.2.2.dmg file from here
Open the dmg file and go to Extras > growlnotify > growlnotify.pkg
Follow the wizard to install growlnotify
Configure app for testing (RSpec, Cucumber with Spork and Guard):
$ rails generate rspec:install
$ rails generate cucumber:install
Configure Spork (for RSpec and for Cucumber)
$ spork --bootstrap
$ spork cucumber --bootstrap
Add environment loading
$ subl spec/spec_helper.rb
Move the entire contents under the Instructions into the Spork.prefork block to enable environment loading only once, and also add:
config.mock_with :rspec
Configure Guard:
$ guard init rspec
$ guard init spork
Edit generated default file so Guard doesn’t run all tests after a failing test passes; drb flag is for Spork to run in distributed Ruby.
$ subl Guardfile
guard 'rspec', :version => 2, :all_after_pass => false, :cli => '--drb' do
Config to run test suite in distributed Ruby
$ subl .rspec
--drb
Start Guard with Spork
$ guard
...and you should get Growl notifications.
The related gems I have in my Gemfile are pretty much the same as yours.
The error says you are missing the spec_helper file. Could you try running rails generate rspec:install and the trying again?
I encountered the same issue and went to Guard's GitHub page to see their documentation. The relevant portion is here: https://github.com/guard/guard#growl
It appears that the simple solution is to include the growl gem in the development group, whereas you've only included it in the test group. I did the following to get it working:
Installed Growl Notify as explained in another answer:
Setup Growl for RSpec test notifications (For Mac): Growl is likely
already installed on the system, but growlnotify probably is not.
Download the Growl-1.2.2.dmg file from here
Open the dmg file and go to Extras > growlnotify > growlnotify.pkg
Follow the wizard to install growlnotify
Updated my Gemfile (relevant part only, the rest is matched to the Rails Tutorial):
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
gem 'guard-rspec', '1.2.1'
gem 'guard-spork', '1.2.0'
gem 'childprocess', '0.3.6'
gem 'spork', '0.9.2'
gem 'growl', '1.0.3'
end
Ran in Terminal:
bundle install
Opened a new terminal window and ran:
guard
Growl started working!