running rspec with "-e" parameter - ruby

I can run the simple ruby program like
ruby -e "puts 'raja'"
but when I run Rspec file, I run this way
ruby rspec my_example_spec.rb
Said that, Do I have any way to pass the rspec program as parameter as I have done in the first line?
I tried
ruby -e rspec "require 'rspec'
require 'watir'
describe 'My behaviour' do
it 'should do something' do
b = Watir::Browser.new
b.goto 'www.google.com'
b.text_field(name: 'q').set 'Rajagopalan'
sleep 2
b.close
end
end"
But it's not running. How can I pass rspec program with '-e' parameter?

Googling a bit, I found this issue thread on the RSpec github: https://github.com/rspec/rspec-core/issues/359
It seems you can run your RSpec tests from Ruby by putting
RSpec::Core::Runner::run({}, $stderr, $stdout)
after your definitions.
The following minimal test works for me:
ruby -e "require 'rspec'
describe 'My behaviour' do
it 'should do something' do
expect(1).to eq(2)
end
end
RSpec::Core::Runner::run({}, \$stderr, \$stdout)
"

Related

Is there any way to run Rspec tests on a single Ruby file?

I would like to run some tests on some code kata exercises that I am working on. Is it possible to run Rspec tests on a single Ruby file? I tried adding require 'rspec' to the top of the file and then the command rsepc from the project dir but the following is returned:
F
Failures:
1) Sentence reverser reverses the words in a sentence
Failure/Error: expect(sentence_reverser(test_sentence)).to eq('I am backwards')
NoMethodError:
undefined method `sentence_reverser' for #<RSpec::ExampleGroups::SentenceReverser:0x0000559764dc5950>
The code I have is:
require 'rspec'
def sentence_reverser str
str.split.reverse.join(' ')
end
describe "Sentence reverser" do
it 'reverses the words in a sentence' do
test_sentence = "backwards am I"
expect(sentence_reverser(test_sentence)).to eq('I am backwards')
end
end
Try running rspec </path/to/kata.rb>. You shouldn't even require 'rspec' then - just tested your example.

pry session during rspec test ends on any input

I am trying to pause an rspec test using binding.pry to understand why it fails. Here is a simplified version:
def copy_db
binding.pry
puts 'hello world'
end
The binding.pry pauses execution but any input in the session causes it to end. Any ideas why this would be? This is not a rails project.
I'm not sure if the previous answer was sufficient, but another thing you could try besides binding.pry is using the byebug gem.
In your Gemfile add:
gem "byebug"
In the terminal (at the root of the project directory) run:
$ bundle install
Require 'byebug' at the top of your code and insert a 'debugger' wherever you want to set a breakpoint:
require 'byebug'
def copy_db
debugger
puts 'hello world'
end
And it will pause rspec and enter the byebug debugger, which lets you display the values of various variables as you step through each line.
[1, 4] in path/to/your/file.rb
1: def copy_db
2: debugger
=> 3: puts 'hello world'
4: end
(byebug)_

Why undefined method `expect' when checking in at_exit hook

I am trying to check the exitstatus of a command through the below code. It is resulting in an error --->undefined method 'expect'
require 'rspec'
require 'rspec/expectations'
at_exit do
\`cat /etc/redhat-release\`
expect($?.exitstatus).to eq(0)
end
Can any one please help me in solving this problem
Your code is suppose be a test case based on rspec, and you are mixing it with normal ruby code.
In order to test your code with rspec you need to wrap the test scenarios and cases inside a describe and it block, only then you'll have access to the expect method, like so:
# test.rb
require 'rspec'
require 'rspec/expectations'
describe 'Test exit status' do
it "check status" do
`cat /etc/redhat-release`
expect($?.exitstatus).to eq(0)
end
end
and you run this code with rspec command line, like: rspec test.rb
The code you had before, was using the at_exit block. This is only called when you exit the program, or receive a signal for it to be killed.

Rake tast with cli arguments in Sinatra

I have created a rake file in my Sinatra app to create indexes for a Mongodb collection and I am trying to pass the environment parameter in the rake task db:create_indexes.
Here is my db.rake file:
namespace :db do
task :create_indexes, :environment do |t, args|
puts "Environment : #{args}"
unless args[:environment]
puts "Must provide an environment"
exit
end
yaml = YAML.load_file("./config/mongoid.yml")
env_info = yaml[args[:environment]]
unless env_info
puts "Unknown environment"
exit
end
Mongoid.configure do |config|
config.from_hash(env_info)
end
Bin.mongoid:create_indexes
end
end
Also the Rakefile in the root of app contains:
require 'rake'
require 'rubygems'
require 'bundler/setup'
Dir.glob('lib/tasks/*.rake').each { |r| load r}
But whenver I try to run the rake task using the command rake db:create_indexes[development], I get the following error, no matches found: db:create_indexes[development]
Now I am clueless about how to solve this issue.
So the problem was not with the code but the shell I was using.
I use zsh shell in place of bash and it seems zsh require you to escape the brackets: rake my_task\['arg1'\].
Therefore the code works with rake db:create_indexes\['development'\].

Running minitest handler tests inside another ruby script

I'd like to run my minitest handler tests inside another ruby script (a bootstrapper script of sorts). I'd like to return the results of my tests to a variable that I can then parse to make sure everything passed before moving on. Is this possible? If so, what does the syntax for something like this look like?
You can shell out to run the test and capture the output.
puts "Running foo test:"
output = `ruby -Ilib:test test/test_foo.rb`
puts output
puts "Completed foo test."
Okay so I figured out how to do this using the rake::test library. This code will execute a minitest test, then slurp in the xml from the report and determine if everything passed or not.
require 'rake'
require 'rake/testtask'
require 'ci/reporter/rake/minitest'
require 'xmlsimple'
Rake::TestTask.new do |t|
t.verbose = true
t.test_files = FileList['system_tests/vm_tests.rb']
end
task :test => :"ci:setup:minitest"
Rake::Task[:test].execute
results = XmlSimple.xml_in('test/reports/TEST-VMtests.xml')
if results["failures"] > 0 or results["errors"] > 0
raise "The VM Tests have resulted in failures or errors"
end

Resources