How to use RSpec from the IRB console? - ruby

I would like to try all the nice oneliners in the documentation of RSpec in the IRB console.
How to execute them in the IRB console?
Please a solution not based in Rails, just plain Ruby.

This might be just what you're looking for:
Standalone
require "rspec/mocks/standalone" to expose the API at the top level (e.g. main) outside
the RSpec environment in a REPL like IRB or in a one-off script.
See https://relishapp.com/rspec/rspec-mocks/v/3-9/docs/outside-rspec/standalone

Related

How to reduce logs from database cleaner in ruby application

Here is my ruby spec_helper for rspec:
As you see I'm using database cleaner because I'm writing tests using the DB.
However, I get all this nonsense in my console output:
Is there a way to supress some of this output? Again remember, I'm not in RAils so I can't simply do:
config.logger.level = Logger::ERROR

ActiveJob instance freezes RSpec test

I have an after_create_commit callback on my model, Foo, which looks like this:
after_create_commit { LogBroadcastJob.perform_later self }
I've reduced my perform method to return nil to simplify things.
When I create a Foo instance in an RSpec test with factory_girl, the test suite freezes. This only happens when I test models with that callback.
FactoryGirl.create :foo
When I Ctl+C out of my test suite it fails to kill the process. I have to find the process, which is still using my database (Postgresql), and kill it, which means that I don't see any errors on the command line. If I run my test suite again, it creates another process that I have to find and kill.
Does this sound familiar to anyone? How would I find useful errors here?
Maybe relevant: I upgraded from Rails 4.2 to 5.0.0.1 a while back.
This was a concurrency issue. Thanks to the resource provided in #coreyward's comment, I was able to clear this up by setting config/environments/test.rb to
config.eager_load = true
This differs from my config in config/environments/development.rb (and everything works in development), so I can't say I understand yet why it works. But I can now run all my tests with bundle exec guard or bundle exec rake spec.

Does ruby has a feature like socksProxy in java?

When I write a program in java, I use -DsocksProxyHost, -DsockProxyPort options to proxy all my network request to the specified address.
Does ruby has a feature like that?
I know there are Proxy class in ruby. However, I do not think I can utilize it when I want to connect to oracle database.
You can do this with JRuby using the -J flag which passes options to the JVM:
jruby -J-DsocksProxyHost=domain program.rb
MRI/Rubinius don't have a similar flag, but you can use a gem like proxifier or socksify to do the dirty work for you.

Setting log levels with Ruby Logger

I'm using a Ruby gem that utilizes the Logger class.
Is there a way to set log levels globally like in log4j with an .xml or .properties file?
I don't want the output to be written at all. My concern is that printing all those logging statements drains performance.
P. S.: It's just a simple script, I'm not using Ruby on Rails.
Because the gem you're using is kind enough to expose the logger as a attr_accessor, you can simply do this:
some_server = Server.new
some_server.logger.level = Logger::ERROR
Obviously feel free to chose the log level that is appropriate for your environment and application (http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html).
Hope this helps.

Preloading classes without Rails?

I am working on a big project, and I realized that several of the components were groups of classes that I could turn into services and strip from Rails. But now that I've done that I realize that the slowness of loading classes without Spork isn't a function of Rails being slow, but a function of Ruby being slow. Is there something like Spork that will work in non Rails projects?
Spork should work just fine for any ruby project, it just requires a bit more setup.
Assuming you're using rspec 2.x and spork 0.9, make a spec_helper.rb that looks something like:
require 'spork'
# the rspec require seems to be necessary,
# without it you get "Missing or uninitialized constant: Object::RSpec" errors
require 'rspec'
Spork.prefork do
# do expensive one-time setup here
require 'mylibrary'
MyLibrary.setup_lots_of_stuff
end
Spork.each_run do
# do setup that must be done on each test run here (setting up external state, etc):
MyLibrary.reset_db
end
Everything in the Spork.prefork block will only be run once (at spork startup), the rest will run on every test invocation.
If you have lots of framework-specific setup, you'd probably be better off making an AppFramework for your library. See the padrino AppFramework for an example.

Resources