How do I run debugger with the Rails run Spec extension? - ruby-on-rails

I am using VsCode to write and edit some rspec tests.
What I would like to be able to do is run a specific 'it' or 'describe' block in debug mode.
At this time I can run the rspec/spec file in debug mode but it executes all of the tests.
I have install 'Rails run Spec' extension which allows me to execute a specific 'it' or 'describe' block without the ability to debug.
Ideally I would like both options married together.
I have done some digging but not able to find anything that fits my scenario.
Any help would be greatly appreciated.
Joe

Add pry-byebug to your gemfile and run bundle install
# Gemfile
gem 'pry-byebug'
Then, whenever you want to inspect a test, add binding.pry inside the test.
# some_spec.rb
it "is not behaving how I want it to" do
binding.pry
expect(my_var).to eq(some_val)
end

Related

Rubymine not executing code inside first Rails test

I've inherited a Rails application, and I'd like to add some unit tests (currently there were just a couple old broken capybara tests). I'm new to Rails so I want to start somewhere very basic. I have a users_controller.rb file, and I've added /test/controllers/users_controller_test.rb with the following code:
require "test_helper"
puts "right before the test"
def test_view_user_info
puts "inside of the test"
get "/users/12/edit"
assert_response :success
end
test_helper.rb just has:
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
class ActiveSupport::TestCase
include Rails.application.routes.url_helpers
end
To run the test in Rubymine, I right-click the test select "Run 'Run test 'users_cont...' (and I also get the same result when I run 'rake test')
The problem: I get exit code 0, but we don't appear to run the code inside the test! I confirmed with with the 'puts' statements- the output only shows the one outside of the test, and not the one inside:
/Users/drkaplan/.rbenv/shims/bundle exec ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -Itest /Users/drkaplan/broad-count-me-in/test/controllers/users_controller_test.rb
right before the test
Process finished with exit code 0
I showed it to someone experienced with Rails who has never seen this before: it's as if my test file is being run as a script and not invoking the test blocks. The gemfile does include capybara in the test group (and I've added mintiest-rails for this new test) so I'm wondering if there may be some conflict between the two that prevents my test code from executing. Here's the relevant part of my gemfile:
group :test do
gem 'shoulda-matchers', require: false
gem 'capybara'
gem 'capybara-email'
gem 'selenium-webdriver'
gem 'minitest-rails'
end
Another weird symptom: when I try running the test individually with --help, I get only the output from puts statements around the test, and none of the typical help options
ruby -I test test/controllers/users_controller_test.rb --help
Anyone see what the problem may be, or had this symptom before?

Calling a ruby script from Rails with different gemfile

I have a ruby script I wrote which generates data and loads it to MongoDB. I am now trying to call this load script from seed.rb of my Rails app (so I can run via rake db:seed)
I attempted to call it from rails using this code:
system( "ruby data_load/db_load.rb -a data_load/doc1.json" )
When that code executes, I get the following error. (Note it runs fine from the command line):
data_load/db_load.rb:15:in `require': cannot load such file -- mongo (LoadError)
from data_load/db_load.rb:15:in `<main>'
The top of db_load.rb looks like this:
# includes gems from gemfile
require 'rubygems'
require 'bundler/setup'
Bundler.setup
require 'mongo'
require_relative 'load_scripts/cmd_options'
require_relative 'load_scripts/build_index'
....
include Mongo
The script has it's own gemfile in the data_load directory.
My guess is ruby is running the script using the bundle for the rails application instead of the shell script.
Any suggestions on how I can execute this script?
I believe the problem is where Bundler is looking for the Gemfile. Since your script is being run in the parent directly it is finding the Gemfile for the main app.
Set the BUNDLE_GEMFILE before calling your script:
system "BUNDLE_GEMFILE=data_load/Gemfile ruby data_load/db_load.rb -a data_load/doc1.json"
I'm sorry but I think you can't do it, unless you run the script as a different process (like a shell command), doing it is easy:
`shell_command params`
Just use the correct path and params.
Otherwise, consider that a gemfile is "more or less" at its basic level, a bunch of require (or load) statements, so loading a different gemfile would overwrite the original one, creating a lot of issue with rails after that.
The subprocess command is a good idea, however you can only pass string as params, not in-memory objects.

How can I configure RSpec to run my model tests?

No matter what I do, the only tests that run with the rake test command are those in spec/requests. Naturally, I would like to run everything in the spec directory.
I thought getting the gem and installing RSpec would do it, but it seems with these testing libraries that the whole "convention over configuration" thing is turned on its head. There's a hell of a lot of configuration.
I simply want to run all of my tests. How can I do that?
What does your Rakefile look like? You may need to add the following:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
Also, check that your spec's filenames all end in _spec.rb.
Just this will do:
bundle exec rspec spec
This must be fired from root directory of application. Here spec is the directory.

simplecov gem code coverage

i use minitest framework for testing and simplecov gem for code coverage. i have a problem about simplecov. my problem is lkie this:
i wrote a model test. when i run the test using rake minitest:models, test runs and coverage shows %100. But when i run tests using bundle exec rake, the code coverage of same tests are shown missing.
i make researching on net. some people also have problem like this about simplecov. but i could not find a solution for this.
i'm waiting your ideas. Thanks in advance.
Did you enabled simplecov by doing SimpleCov.start on top of your code?
This is required as the first statement i.e. before your code, otherwise you will never get it work.
Also include the SimpleCoV Adapter.
As following the post generating-code-coverage-metrics-for-a-ruby-on-rails-project-with-simplecov, define rules with SimpleCov putting conditions:
SimpleCov.start do
# rules here
end if ENV["COVERAGE"]
Then run the coverage on demand by specifying the coverage variable:
COVERAGE=true bundle exec rake spec

How to combine autotest and spork in Rails testing?

Autotest increases the speed at which tests run by running only the changed tests.
But I want to push it even further by using spork to preload the Rails environment, so that I will get even faster feedback.
Is this possible?
Autotest : https://github.com/grosser/autotest
Spork : http://github.com/timcharper/spork
ARTICLE1 mikbe has you covered! I would attempt to rephrase it here, but the post does such a great job.
If you happen to be on OSX, there are also instructions to utilize Growl for tray notifications.
ARTICLE2 Ruby Inside also has a walkthrough for Rails 3 and RSpec 2.
If you use Ruby 1.9 and want to use spork and autotest together with Test::Unit (actually MiniTest) try this:
Gemfile:
group :test do
# Newer version of test::unit:
gem 'minitest'
# spork preloads a rails instance which is forked every time the tests are
# run, removing test startup time.
gem 'spork'
# Run 'spork minitest' to start drb server (test server). Use 'testdrb' to
# run individual tests via spork.
gem 'spork-minitest'
# Run 'bundle exec autotest' to rerun relevant tests whenever a file/test is
# changed. '.autotest' makes sure the tests are run via test server (spork).
gem 'autotest-standalone'
# -pure gives us autotest without ZenTest gem.
gem 'autotest-rails-pure'
end
.autotest:
class Autotest
# run tests over drb server (spork)
def make_test_cmd files_to_test
if files_to_test.empty?
"" # no tests to run
else
"testdrb #{files_to_test.keys.join(' ')}"
end
end
end
(Note: Instructions says bin/testdrb, but I changed it to testdrb to make it work for me.)
In a terminal:
spork minitest --bootstrap
Edit test/test_helper.rband follow instructions.
After the above setup is done once, you can start the test server:
spork minitest
Finally start autotest in another terminal:
bundle exec autotest
And (hopefully) enjoy really fast autotesting with MiniTest.
I haven't tried it yet, but there's a section in chapter 3 of the RailsTutorial that tells some "hacks" to set up spork. The tutorial currently says:
... as of this writing Spork doesn’t officially support Rails 3
The chapter goes on to tell how to set it up with autotest. One thing to know is that you'll need
--drb
in your .rspec file.

Resources