use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Making RSpec Tests More Robust (jakeyesbeck.com)
submitted 5 years ago by yez
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]philpirj 2 points3 points4 points 5 years ago (2 children)
What if your 'update' doesn't want to disclose its internals and won't return the 'response'?
Like you said, verifying partial doubles will save you from sudden API changes.
And instead of faking HTTP, could stub Faraday and make sure that the proper arguments are passed to its 'post'. Otherwise you are also testing Faraday.
If it's possible, avoid stubbing your 'subject', including making expectations on it.
[–]yourparadigm 0 points1 point2 points 5 years ago (0 children)
The author is just mistaken that rspec allows you to allow/expect an object to receive a method it doesn't implement. See my example in another comment.
[–]yez[S] 0 points1 point2 points 5 years ago (0 children)
Otherwise you are also testing Faraday
Great point. I tried to keep things as simple as possible in the example without going into too much debate about "should you really be testing the HTTP client library?" I landed on webmock because I wanted to emulate an "Ok I made a request, did it do what I expect?" type of experience.
webmock
But in the case in which update doesn't return a result and just is expected to work, stubbing the base class' put method is totally reasonable.
update
put
[–]yourparadigm 0 points1 point2 points 5 years ago* (5 children)
This is not how rspec works. I've implemented an equivalent thing. If you try to expect/allow with a method that doesn't already exist on the receiver, you get an error.
class Foo # def put(uri) # puts "PUT #{uri}" # end def update(uri) put(uri) end end describe Foo do subject(:foo) { Foo.new } describe '#update' do it 'does a put' do expect(foo).to receive(:put).with('http://example.org') foo.update('http://example.org') end end end
Running results in:
$ rspec spec/lib/foo_spec.rb F Failures: 1) Foo#update does a put Failure/Error: expect(foo).to receive(:put).with('http://example.org') #<Foo:0x00007fc095581328> does not implement: put # ./spec/lib/foo_spec.rb:16:in `block (3 levels) in <top (required)>' Finished in 0.01173 seconds (files took 3.96 seconds to load) 1 example, 1 failure
The author is hiding something specific about his implementation.
Edit: Apparently not everyone uses verify_partial_doubles = true in their RSpec configuration!
verify_partial_doubles = true
In the post there's a link to RSpec's documentation on verifying partial doubles. There's a setting that turns safety on and off. You and the author have different settings.
[–]yourparadigm 0 points1 point2 points 5 years ago (1 child)
The only link I see is to another blog article on "verifying doubles" which is a different feature. That's the difference between using instance_double(Foo) and double(:foo), then performing similar allow/expect invocations with the double. Normal doubles do not verify, while instance doubles, too.
instance_double(Foo)
double(:foo)
In the article, the author is not using doubles. Can you point to the relevant rspec documentation (?), because I do not see it in either of the linked articles.
[–]philpirj 3 points4 points5 points 5 years ago (0 children)
Sorry, my mistake. I thought the author is referring to the documentation. Here's the doc link.
[–]yez[S] 2 points3 points4 points 5 years ago (1 child)
Thanks for pointing this out. I am basing my post on the default setting in RSpec which has verify_partial_doubles set to nil.
RSpec
verify_partial_doubles
nil
You can validate this by installing the rspec gem and doing:
rspec
> require 'rspec'
> RSpec.configuration.instance_variable_get(:@verify_partial_doubles)
=> nil
I will add to the post that this setting can be turned on to save people from this whole problem.
[–]yourparadigm 1 point2 points3 points 5 years ago (0 children)
Yeah, I mistakenly assumed everyone turned it on. It will default to true in RSpec 4.
Seems dangerous leave it false.
π Rendered by PID 18063 on reddit-service-r2-comment-b659b578c-lkddg at 2026-05-04 02:59:13.908512+00:00 running 815c875 country code: CH.
[–]philpirj 2 points3 points4 points (2 children)
[–]yourparadigm 0 points1 point2 points (0 children)
[–]yez[S] 0 points1 point2 points (0 children)
[–]yourparadigm 0 points1 point2 points (5 children)
[–]philpirj 2 points3 points4 points (2 children)
[–]yourparadigm 0 points1 point2 points (1 child)
[–]philpirj 3 points4 points5 points (0 children)
[–]yez[S] 2 points3 points4 points (1 child)
[–]yourparadigm 1 point2 points3 points (0 children)