Paperclip using rspec - ruby-on-rails

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.

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`

Errors using rspec and capybara

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.

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

How do I setup RSpec with a gem that has an activerecord model in it?

We are extracting a few models into a gem so it can be shared among a couple different services and we aren't able to get rspec running in the gem. When we require the gemname in the spec_helper it errors saying:
uninitialized constant Object::ActiveRecord
Some of the file contents are below. Any ideas?
Right now the spec helper is as follows:
require 'rubygems'
require 'bundler/setup'
RSpec.configure do |config|
Bundler.require(:default,"test")
require 'tup-user' # and any other gems you need
end
Our Gemfile is:
source :rubygems
# Specify your gem's dependencies in tup-user.gemspec
gemspec
gem 'rspec-rails'
gem 'aasm'
gem 'authlogic'
gem 'rails'
The line the error is being thrown on is the first of the class:
class User < ActiveRecord::Base
(See comments to question) After getting some good advice on the ruby-talk mailing list, I now require any libraries a file needs within the file itself.

RoR: Testing: undefined local variable or method `be_nil'… but… I included RSpec, spec/spec_helper, spec/matchers, etc

in my environments/test.rb:
config.gem "rspec"
config.gem "rspec-rails"
relevant part of my gemfile:
group :test do
...
gem "rspec", "1.3.2"
gem "rspec-rails", "1.3.2"
in test_helper.rb
require 'test_help'
require "bundler/setup"
Bundler.require(:test)
require 'factory_girl'
require 'shoulda'
require "shoulda-matchers"
require 'spec'
require 'spec/matchers'
I recently moved all my gems to a gemfile, instead of having everything in config.gem's
in my env/test.rb file, I have to use the two config.gem's or my tests don't run at all.
I've read that including spec_helper will help.... but my tests aren't in the spec folder... they are in the test folder... when I include spec/spec_helper.rb, all of my tests get WARNING: already defined.
EDIT: I found this link: https://github.com/rspec/rspec-rails/issues/93
which told me to do
include RSpec::Matchers
and I got a differente error with this one:
/Users/lprestonsegoiii/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/optparse.rb:314:in `initialize': unhandled exception
EDIT2:
I've discovered through using IRB, that the minimum to use the matchers are:
require "spec/spec_helper"
include RSpec::Matchers
But I still have the duplicate definition issue. hmm.

Resources