RSpec: stubbing Kernel::sleep?
If you are calling sleep within the context of an object, you should stub it on the object, like so:
class Foo
def self.some_method
sleep 5
end
end
it "should call sleep" do
Foo.stub!(:sleep)
Foo.should_receive(:sleep).with(5)
Foo.some_method
end
The key is, to stub sleep on whatever "self" is in the context where sleep is called.
How to test kernel sleep method in module in Rspec 3.5?
I believe it's not working because you're not putting the expectation on the right thing. It looks like you've tried putting it on Kernel
, and on the class under test, but you need to put it on the instance:
it 'sleeps retry api calls' do
thing = klass.new
allow(thing).to receive(:sleep)
thing.test_add_api_delay
expect(thing).to have_received(:sleep).with(1)
end
The above has a test smell in that it is stubbing the class under test. But I think it's probably better than enforcing some design constraint here and losing some of Ruby's elegance in just calling sleep
.
Stub sleep with RSpec
You should stub sleep on the object it is called on
Eg.
class SleepTest
def sleep_method
sleep 2
end
end
And in your test
sleep_test = SleepTest.new
allow(sleep_test).to receive(:sleep)
stubbing a class method inside a method in rspec to return true
Ensure the syntaxing of mocking the return value is correct:
allow(SiteLicenseService).to
receive(:get_package_for_site_license).and_return(true)
In Ruby, classes are written in TitleCase and methods are written in snake_case. The method to be received should be a :symbol.
RSpec how to stub open?
I found a solution here on Stack Overflow after some more time on Google (I can't believe I didn't find this before).
Explanation taken from here and written by Tony Pitluga (not linkable).
If you are calling sleep within the context of an object, you should stub it on the object[...]
The key is, to stub sleep on whatever "self" is in the context where sleep is called.
So I did this and it all worked out:
let(:read) { mock('open') }
it "should return the new log-level when the log level was set successfully" do
read.stub(:read).and_return('log-level set to 1')
kannel.should_receive(:open).and_return(read)
kannel.set_log_level(1).should == 1
end
RSpec Stubbing out Date.today in order to test a method
Add this to your before block
.
allow(Date).to receive(:today).and_return Date.new(2001,2,3)
I *think* it is not necessary to unstub.
Rspec 3 stubbing user input
In order to silence test results from appearing in the command line when you run specs, you can stub out $stdout
's output stream:
before do
allow($stdout).to receive(:write)
end
In order to send a return character with your stubbed input, you'll need to provide it with a newline character:
allow(greeting).to receive(:gets).and_return("Brian\n")
Related Topics
I Have a Rails Task: Should I Use Script/Runner or Rake
How to Print Out the Contents of an Object in Rails for Easy Debugging
Flattening Nested Hash to a Single Hash with Ruby/Rails
Installing Jekyll on Ubuntu 14.04
Typing 'Rails Console' Doesn't Start
How to Install JSON Gem - Failed to Build Gem Native Extension(MAC 10.10)
Generate a Powerset of a Set Without Keeping a Stack in Erlang or Ruby
Merging Multi-Dimensional Hashes in Ruby
Understanding the Gemfile.Lock File
How to Merge Two Hashes Without Overwritten Duplicate Keys in Ruby
Setting Elastic Search Limit to "Unlimited"
How to Combine Overlapping Time Ranges (Time Ranges Union)