2

I am placing a few puts statements in my script. But the messages are not printed; the script is passing through these statements. I observe the moment execution goes into rake puts is not working throughout the flow.

In spec helper

class Testbed
puts "Inside test bed" // This puts is printed
some code
some code
some code
some code
end

RSpec.configure do |config|
  puts "In side rspec config" // This puts is printed
   config.color = true
   config.tty = true
some code
some code
some code
some code
end

config.before(:all) do
    puts "in before all"//not printed

None of the puts statements here is printing an output. My puts in Page Object, Spec files are not considered.

Can any one suggest a workaround for this?

Justin Ko
  • 46,526
  • 5
  • 91
  • 101

2 Answers2

4

Consider where puts outputs by default. It uses $stdout unless you've told it to use a different channel, such as:

File.open('foo', 'w') do |fo|
  fo.puts 'bar'
end

When it's using $stdout or STDOUT, a calling program, especially one that's using Ruby in a sub-shell, can capture the output and do what it wants with it. That's a feature of the OS. If I had code this code saved in 'test.rb':

puts 'foo'

and called it from the shell:

ruby /path/to/test.rb > /some/file.txt

The output would be piped to /some/file.txt and I wouldn't see it at the console. Similarly, if I had a second app that called the first:

output = %x[ruby /path/to/test.rb]

The variable output would contain the text 'foo', captured from the sub-shell's STDOUT being rerouted.

There are a lot of ways to do this, from using backticks or %x[...] to using popen or pipes in a regular open command to the methods in Open3. Discussing filenames when opening a file, the IO docs say:

A string starting with "|" indicates a subprocess. The remainder of the string following the "|" is invoked as a process with appropriate input/output channels connected to it.

So, asking us why puts isn't outputting to your console isn't easily answered, but you can see it's easily done if something is calling your code besides Ruby directly.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • How can I print a variable? `branchName = options[:name] puts(message: "Creating a new branch named:")`. I want to print the branchName variable in this statement. – nr5 Aug 09 '19 at 07:51
0

Can any one suggest a workaround for this?

First, how about paying attention to all your errors?

`<main>': undefined method `config' for RSpec:Module (NoMethodError)

Your code does this:

RSpec.configure do |config|
  puts "In side rspec config" // This puts is printed
  config.color = true
  config.tty = true
end

...and the config variable is only defined inside the block. But then you write this:

config.before(:all) do
  puts "in before all"//not printed
end

That code isn't inside the block, so the config variable doesn't exist.

Next, before() only executes before other tests--but you have no tests, as the rspec output shows:

$ rspec 1.rb 
Inside test bed
In side rspec config
No examples found.  #*********HERE


Finished in 0.00032 seconds (files took 0.0879 seconds to load)
0 examples, 0 failures   #*******HERE

Try this:

class Testbed
  puts "Inside test bed" #This puts is printed
end

RSpec.configure do |config|
  puts "In side rspec config" #This puts is printed
  config.color = true
  config.tty = true

  config.before(:all) do
    puts "in before all" #?????
  end
end

#Here is an rspec test:

RSpec.describe do
  it "some test" do
  end
end

...

~/ruby_programs$ rspec 1.rb 
Inside test bed
In side rspec config
in before all   #Yeah!!
.

Finished in 0.00073 seconds (files took 0.4294 seconds to load)
1 example, 0 failures
7stud
  • 46,922
  • 14
  • 101
  • 127