Rspec SimpleCov NoMethodError - ruby-on-rails

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.

Related

uninitialized constant (NameError)

I have seen few similar questions here on SO but none of the given solutions work. I have installed rspec/factorygirl as usual and i'm facing some problems i can't understand. It seems that rspec can't see my models.
Gefile
group :development, :test do
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
gem 'database_cleaner'
gem 'faker'
end
group :test do
gem 'guard-rspec'
gem 'capybara'
end
I have run rails generate rspec:install to generate the spec/ dir file structure (so my spec_helper and rails_helper files contains the proper lines with environment require etc.)
spec/models/list_spec.rb
require 'spec_helper'
describe List do
before(:each) do
#list = FactoryGirl.build(:list)
end
it 'is invalid without name' do
#list.title = nil
expect(#list).to_not be_valid
end
end
spec_helper.rb
https://gist.github.com/mbajur/68e96ab77f43d50a73cf
rails_helper.rb
https://gist.github.com/mbajur/677c9936347be8b18c7d
list_factory.rb
https://gist.github.com/mbajur/49668f280891fa80f288
And when i run rspec, it gives me uninitialized constant List (NameError) on line 3 of list_spec.rb file.
Change describe List do ... to describe 'List' do ... and be sure what you require all needed lib in helper file and leave one _helper.rb.
Problem was caused by the fact that i was requireing spec_helper in all my specs while i should require rails_helper.
So, each spec file should start with the following line:
require 'rails_helper`

Difference between factory_girl and factory_girl_rails Ruby Gems

Is there a difference between factory_girl and factory_girl_rails Ruby Gems? I have a recurring problem with an error in RSpec Testing: "uninitialized constant FactoryGirl (NameError)".
Somebody told me that there is a difference between the two (this is really confusing) and one needs the other to work or something along those lines?
My spec_helper file has both:
require 'factory_girl'
require 'factory_girl_rails'
My Gemfile has:
gem 'factory_girl_rails'
Here is the full error:
uninitialized constant FactoryGirl (NameError)
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `each'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22:in `run'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:80:in `run'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'
****#epi-stu-hut-shell3:~/projects/project4/spec/factories$
You only need:
group :development, :test do
gem "rspec-rails"
gem "factory_girl_rails"
end
because factory_girl_rails automatically incorporates the factory_girl gem and adds support for Rails.
These gems go in both test and development groups because Rails generators will create stub files in development and of course they are needed in the test environment.
There's no need to add factory_girl or factory_girl_rails to your specs/spec_helper.rb file.
Add factory_girl_rails to your Gemfile under test group
group :test, :development do
gem "factory_girl_rails"
end
then do NOT do a require in spec_helper, it should get loaded automatically during tests; use bundle exec if needed when running rspec

After hook error in rspec

require 'spec_helper'
describe StudentsController do
fixtures :all
context "#profile" do
let(:school) { Factory.create(:school_2) }
before do
session[:email] = user.email
session[:user_id] = user.id
end
it "should assign the user to #user" do
User.stub(:find).with(user.id).and_return(user)
get :profile
assigns[:user].should eql(user)
end
end
end
An error occurred in an after hook:
NoMethodError: undefined method `original_path_set' for nil:NilClass
occurred at /home/qbadmin/.rvm/gems/ruby-1.9.3-p194/gems/rspec-rails-2.13.1/lib/rspec/rails/view_rendering.rb:121:in `block (2 levels) in <module:ViewRendering>'
This is the error I got when running rspec spec from the command line. Can anyone help me to figure out what the problem is?.
The RSpec versions I have specified in the Gemfile are:
gem 'rspec', '~> 2.13',
gem 'rspec-rails', '~> 2.13'
Thanks in advance.
In your gemfile you should have something like this in order for rspec to work correctly
group :test, :development do
gem 'rspec-rails', '~> 2.13.2'
end
I also recommand you take a look at the database_cleaner gem
group :test do
gem 'database_cleaner', '~> 1.0.1'
end
Regarding your issue, after little research, it seems there is an issue with how rspec is getting the path of your spec file. Can you confirm that the above specs are written in the following file ?
spec/controllers/students_controller_spec.rb
Could you also copy / paste your controllers code please ?
I had this exact issue to day.
It turned out I had forgotten to do a rake db:test:prepare after a database change, before running the test suite again.

Using Rspec and Shoulda, why is 'setup' undefined?

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

NoMethodError: undefined method `mock' with Mocha and Rails 3

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!

Resources