Why undefined method `expect' when checking in at_exit hook - ruby

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.

Related

running rspec with "-e" parameter

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)
"

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.

Could not find 'rspec/autorun'

I'm trying to do the first example of The rspec book
greeter_spec.rb
class RSpecGreeter
def greet
"Hello RSpec!"
end
end
describe "RSpec Greeter" do
it "should say 'Hello RSpec!' when it receives the greet() message" do
greeter = RSpecGreeter.new
greeting = greeter.greet
greeting.should == "Hello RSpec!"
end
end
When I run $ rspec greeter_spec.rb the output should be something like this:
.
Finished in 0.00075 seconds
1 example, 0 failures
but I got:
Could not find 'rspec/autorun'
This may happen if you're using rubygems as your package manager, but it is not
being required through some mechanism before executing the rspec command.
You may need to do one of the following in your shell:
# for bash/zsh
export RUBYOPT=rubygems
# for csh, etc.
set RUBYOPT=rubygems
For background, please see http://gist.github.com/54177.
I tried to include require 'rspec/autorun' at the top of the file but doesn't work, also I did what they suggest on the output but still not working.
I'm using ruby version 2.0.0p648, and rspec 2.0.0

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

How do I prevent RSpec from concealing missing dependencies?

I have a Ruby program that fails at runtime, but works when I test it with RSpec. I know the cause of the bug and how to fix it (see below), but I can't figure out how to build a failing RSpec test which proves the existence of the bug.
Imagine the following Ruby:
foobar.rb
class Foobar
attr_reader :fruit
def initialize
#fruit = Set.new ["Apple", "Banana", "Kiwi"]
end
end
The above code uses a Set, but it fails to "require 'set'". This causes it to fail at runtime:
$ irb
> require './foobar.rb'
> f = Foobar.new
NameError: uninitialized constant Foobar::Set
Before fixing the oversight, I wanted to build a simple RSpec test that proves the bug. My test looks like this:
foobar_spec.rb
require 'rspec'
require './foobar.rb'
describe Foobar do
it "can be initialized" do
expect { Foobar.new }.to_not raise_error
end
end
Running the test, I was surprised to see that it passes:
$ rspec foobar_spec.rb
.
Finished in 0.00198 seconds
1 example, 0 failures
After a little digging, I learned that RSpec loads Set for itself. This has the consequence of making Set available to the code it tests, and in my case concealing a bug.
I had the idea of "unloading/unrequiring" Set in my test. The closest I came was this code:
Object.send(:remove_const, :Set)
That indeed causes the test to fail, but unfortunately it also prevents Set from being loaded again by a future 'require', meaning it continued to fail even after I added require 'set' inside foobar.rb.
Is there a better way to unload gems at runtime? If not, what can I do to make this test fail as it should?
require 'rspec'
describe 'foobar.rb' do
it "can instantiate Foobar" do
`ruby -e 'Foobar.new' -r./foobar.rb`
$?.exitstatus.should == 0
end
end
works for the one case you mentioned. That said, I wouldn't recommend this approach. To cover all the cases where a class is referenced, you'd need to run all your specs this way, since the class reference could appear anywhere in your code.

Resources