Integrate Ruby-Lint with Rspec - ruby-on-rails

I have a Rails 4.2 project that uses Bundler and Rspec. I want ruby-lint to work with my specs. By default it doesn't:
$ ruby-lint spec/models/user_spec.rb
user_spec.rb: error: line 3, column 1: undefined constant RSpec
user_spec.rb: error: line 4, column 3: undefined method describe
user_spec.rb: error: line 5, column 5: undefined method let
I have require 'rails_helper' in spec/models/user_spec.rb so it is natural to expect that ruby-lint would load it and therefore load rspec.
What should I do in order to make ruby-lint handle spec files correctly?
P.S. I don't have any specific ruby-lint configuration.
P.P.S. I have the same question for the Gemfile file.
P.P.P.S. Actually I need it in order to get error checking in vim via synstastic or neomake.

I have only used ruby-lint sparingly (because of the many false negatives it gives). So I am no expert unfortunately.
I suppose you want to use it in some automated way, because from the command line you can just point it to the directory that you need.
Looking at the code you can put the directories that you want to lint in a config file. I think something like the following entry should work:
directories: ['lib']
See example config.yaml and example rspecs and of course the relevant config code.

Related

RSpec Stubbing Java Static Method ArgumentError

I am attempting to stub a Java static method in my specs for my JRuby class which imports some Java libraries. I get the following error for the method call:
Failure/Error: JCoDestinationManager.getDestination('properties')
ArgumentError:
Wrong number of arguments. Expected 0, got 1.
That's the JCo Java static method. I created a small spec file to try and isolate the issue:
require 'rails_helper'
describe SapClient do
let(:destination) { double(Java::ComSapConnJcoRt::RfcDestination) }
before do
allow(JCoDestinationManager).to receive(:getDestination).and_return destination
end
it 'can be created' do
c = SapClient.new
expect(c).to_not be_nil
end
end
If I add the with clause like so, I still get the same result.
allow(JCoDestinationManager).to receive(:getDestination).with('properties').and_return destination
The strange part to me is that I only encounter this project in rails. If I copy the code over to a Sinatra project and run these specs there, all is well. I created a new Rails project and a new Sinatra project and installed the same versions of RSpec, verified RSpec mocks were the same version, etc. and still saw the discrepancy in behavior.
When I throw in a pry and observe what happens when I just call
JCoDestinationManager.getDestination
With no arguments, I get the mock destination that I define. If I comment out the allow statement, I see a real RfcDestination get created.
While searching, only thing I saw that seemed close to what I'm observing here is from https://github.com/rspec/rspec-mocks/issues/919 but apparently the solution to this problem was a jruby issue that has since been fixed.
Rails Gemfile
Rails Gemfile.lock
Sinatra Gemfile
Sinatra Gemfile.lock

RSpec does not see classes from /custom rails directory

I've created my own service (which is Plain Old Ruby Object) in app/services/nested/auth_service.rb. Of course I decided to write tests for it, so I created file in RSpec under spec/services/nested/auth_service_spec.rb and wrote inside:
require 'spec_helper'
RSpec.describe Nested::Auth do
end
I added spec_helper because Nested::Auth is just PORO without any rails stuff inside (so rails_helper would be overkill here, right?)
Unfortunately while running rspec command I got error: uninitialized constant Nested::Auth
I added $: << '../app/services' at the top of spec_helper.rb but didn't help. $: << '../services' also didn't work.
How to let spec_helper see /services directory? How to fix uninitialized constant error?
Just follow Rails convention and name your services with Service postfix.
This way Nested::AuthService. Works perfectly now.
In the pasted error message, the constant is misspelled "uninitialized constant Nasted::Auth" where "Nasted" has an "a" instead of an "e". Check any of your files for the misspelling and fix it if you find anything.

Modularity and tables on Rspec (RoR)

I'm doing an Rspec feature to test some user story and I'm getting the error message:
Internal Server Error uninitialized constant Tree::MY_BRAnCH
Now, I know the test fails because the table "trees" doesn't have the proper rows but only fails when I run the suite test.
RAILS_ENV=test bundle exec rspec spec/
pointing to the articles_spec.rb file as responsible. But if I run just the feature file:
RAILS_ENV=test bundle exec rspec spec/features/articles_spec.rb
the test pass fine. Digging in the code I see other developer made a test with the indication:
before { truncate(Tree) }
So that test is executed first and is removing the data in the table.
My question is: how can avoid this? Need I to reload all the database before each rspec file?
or what policy should we follow to be sure the rspec tests are not affecting other developers?
It seems unlikely that truncating a database table would cause an uninitialized constant error. More likely, articles_spec.rb causes Tree::MY_BRAnCH to be defined. That's why running articles_spec.rb alone passes. When you run the whole spec suite, something tries to use the constant before it has been defined, hence uninitialized constant.
One solution could be to search your codebase for usages of Tree::MY_BRAnCH and make sure that it has been defined before it is used. You may need to learn about one or more of the following code loading techniques:
Kernel#require
Kernel#autoload
ActiveSupport::Autoload

How to access Rails test helper code from the console?

Using vanilla ActiveSupport::TestCase, I've added several helper methods that generate test model objects in test/test_helper.rb. In this file is a reopening of ActiveSupport::TestCase that contains the helper methods.
Now suppose I'm in the Ruby Console (or pry, with the Rails environment loaded), and I want to fiddle with these test methods. I can load the file: load "test/test_helper.rb" but I can't create a TestCase instance that will let me access those test methods to play with them:
[4] pry(main)> tc = ActiveSupport::TestCase.new
ArgumentError: wrong number of arguments (0 for 1)
[5] pry(main)> tc = ActiveSupport::TestCase.new(1)
TypeError: 1 is not a symbol
[6] pry(main)> tc = ActiveSupport::TestCase.new(:unit)
ArgumentError: uncaught throw :invalid_test
I've tried in vain to follow the trail of breadcrumbs in source files to find the TestCase initialize method so I could figure out what it's insisting on here.
Can someone help me with how to think about problems like this and figure out a workaround?
Copied from this link: https://gist.github.com/1297510
On your terminal type:
RAILS_ENV=test pry
For your specific case, in my version of ActiveSupport (3.22.2.8), I was able to do the following:
ActiveSupport::TestCase.new('some_string')
In general though, there's several options:
using a debugger like pry can be helpful, to step through the issue.
find the source code via gem open activesupport or find where your gems are installed with gem environment
most gems are open source and are easily found on github.com, etc. Just be careful that you're looking at the correct version.

Rails + RSpec problem

I have just installed Rspec and Rspec-rails. When i try to run the test, it says:
rake aborted!
Command /opt/local/bin/ruby -I"lib" "/opt/local/lib/ruby/gems/1.8/gems/rspec-1.3.0/bin/spec" "spec/controllers/free_controller_spec.rb" --options "/Volumes/Trash/dev/app/trunk/spec/spec.opts" failed
Full log here: http://pastie.org/939211
However, my second "test" application with sqlite works with it. I think the problem is in my DB.
My ruby version is 1.8.7, i use mysql as database.
My files:
specs/spec_helper.rb
config/environment.rb
config/environments/test.rb
List of my gems
My test is just:
require 'spec_helper'
describe FreeController do
it "should respond with success" do
get 'index'
response.should be_success
end
end
I really can't understand the error, so i don't know how to fix it..
Additional question: should i use a fixtures and ActiveRecord, if i going to use Machinist for creating test data? What should i do to disable them?
From your error log:
/app/models/thread.rb:1: superclass mismatch for class Thread (TypeError)
Is your model named Thread? You might have a name collision. Ruby has built-in class named Thread. Try renaming your model.

Resources