Test for warnings using RSpec
warn
is defined in Kernel
, which is included in every object. If you weren't raising the warning during initialization, you could specify a warning like this:
obj = SomeClass.new
obj.should_receive(:warn).with("Some Message")
obj.method_that_warns
Spec'ing a warning raised in the initialize
method is quite more complex. If it must be done, you can swap in a fake IO
object for $stderr
and inspect it. Just be sure to restore it after the example
class MyClass
def initialize
warn "Something is wrong"
end
end
describe MyClass do
before do
@orig_stderr = $stderr
$stderr = StringIO.new
end
it "warns on initialization" do
MyClass.new
$stderr.rewind
$stderr.string.chomp.should eq("Something is wrong")
end
after do
$stderr = @orig_stderr
end
end
How can I test in RSpec whether code I've loaded has any Ruby warnings?
I ended up initiating a new process to and evaluate the process output. In my case the issue was a redefined constant so I'm testing by loading the class twice into the new Ruby process and then using IO stream to read the output:
it 'should not redefine BUILD_FILE constant' do
root_path = "#{File.dirname(__FILE__)}/../../../../"
cmd = "load \"#{root_path}libraries/build_file.rb\";"
ruby_path = 'C:/opscode/chefdk/embedded/bin/ruby.exe'
pipe_cmd_in, pipe_cmd_out = IO.pipe
pid = Process.spawn("#{ruby_path} -e '#{cmd} #{cmd}'", :out => pipe_cmd_out, :err => pipe_cmd_out)
Process.wait(pid)
pipe_cmd_out.close
output = pipe_cmd_in.read
puts "OUTPUT = #{output}"
expect(output).to_not match(/previous\sdefinition\sof\sBUILD_FILE/)
end
Rspec rails printing lot of warnings
removing --warnings
option from your .rspec
file would do the trick
Guard with RSpec on Rails 4 giving a lot of warnings
Rspec: How to suppress warnings and notices when running tests?
Did you set: config.use_transactional_examples = true
to false and see if that breaks anything?
When I run the test rspec spec In the console, I'll get a deprecation warning
Write the test in a new style:
expect(item.price).to eq 212
BTW. it seems you might doing sth quite risk/confusing. Once you assign 200 to the attribute, it will be more than confusing to see another value returned by a getter with a same name. Have you considered leaving the original method alone and defining a new one instead (like price_with_vat)?
deprecation errors when running rspec tests after updating to rails 5.0.0
The warnings were caused by the wicked_pdf
gem, updating to version 1.1.0
solved the issue
How to Suppress Warnings in RSpec while running Ruby 2.7.0
These warnings/deprecations are directly from the ruby 2.7, you cannot silence them by rails or rspec configuration.
Ruby 2.7 is trying to warn you about backward incompatibilities that will arrive in ruby 3.0. See the release notes. Main source of deprecations is the Separation of positional and keyword arguments part.
Rails and other libraries and gems are not prepared for this change yet, so ruby is showing you tons of warnings.
I would wait with upgrade until gems resolve these warnings in the future, but you can also suppress these warnings according to following article https://prathamesh.tech/2019/12/26/managing-warnings-emitted-by-ruby-2-7/
RUBYOPT='-W:no-deprecated -W:no-experimental' rails c
Related Topics
How to Understand the #Dup and #Clone Operate on Objects Which Referencing Other Objects
Convert Spreadsheet Column Index into Character Sequence
How to Count Duplicates Hash Itens in Ruby 1.8.5 ( Sketchup Ruby API )
How to Ignore File Types in a Web Crawler
Searching from a Range of Ids in Activerecord
Where Are Catch and Throw Useful in Ruby
Ruby Getting Deeply Nested JSON API Data
How to Combination/Permutation in Ruby
Passing Variables on the Command Line to a Cucumber Test
Gitlab: Invocation of Gitlab-Shell
How to Fill Out Login Form with Mechanize in Ruby
How to Iterate Through This JSON Document Using Ruby
How to Access Text Field in an Iframe
How to Specify Formatting Options for To_Yaml in Ruby
Defined' and 'Unless' Not Working as Expected