Structs and expect(o).to receive(:method)

125 views
Skip to first unread message

Jack Royal-Gordon

unread,
Feb 25, 2021, 4:38:25 PM2/25/21
to rs...@googlegroups.com
I’m seeing unusual behavior when i place an "expect().to receive” on an element of a struct. Specifically, with the following code:

  describe 'test example of struct' do
    let!(:subscription) { Sub = Struct.new(:canceled_at) ; Sub.new(canceled_at: 2.weeks.from_now) }

    it 'behaves okay with no expectation set' do 
      value = 2.weeks.ago
      subscription.canceled_at = value
      puts subscription.inspect
      expect(subscription.canceled_at).to eq value
    end
    
    it 'misbehaves if I place an expecation on the element' do
      expect(subscription).to receive(:canceled_at)
      value = 2.weeks.ago
      subscription.canceled_at = value
      puts subscription.inspect
      expect(subscription.canceled_at).to eq value
    end
  end

Here is the result of running those examples:

Run options: include {:locations=>{"./spec/models/user/stripe_customer_spec.rb"=>[362]}}
#<struct Sub canceled_at=Thu, 11 Feb 2021 21:24:19 UTC +00:00>
./Users/jackrg/Documents/Novelty-Stats/spec/models/user/stripe_customer_spec.rb:363: warning: already initialized constant Sub
/Users/jackrg/Documents/Novelty-Stats/spec/models/user/stripe_customer_spec.rb:363: warning: previous definition of Sub was here
#<struct Sub canceled_at=Thu, 11 Feb 2021 21:24:19 UTC +00:00>
F

Failures:

  1) User::StripeCustomer test example of struct misbehaves if I place an expecation on the element
     Failure/Error: expect(subscription.canceled_at).to eq value
     
       expected: 2021-02-11 21:24:19.601491000 +0000
            got: nil
     
       (compared using ==)
     # ./spec/models/user/stripe_customer_spec.rb:377:in `block (3 levels) in <top (required)>'

Finished in 0.0432 seconds (files took 7.91 seconds to load)
2 examples, 1 failure


The only difference between the two examples is the addition of the highlights line in the second example. That expectation causes the retrieval process to not work, returning nil instead of the current value (or even the initial value).

Is this a known issue? I can’t find it documented anywhere.

Phil Pirozhkov

unread,
Feb 25, 2021, 4:51:46 PM2/25/21
to Jack Royal-Gordon
expect(subscription).to receive(:canceled_at)

is an equivalent to

expect(subscription).to receive(:canceled_at).and_return(nil)

and no wonder

expect(subscription.canceled_at).to eq value

fails

You are probably looking to

expect(subscription).to receive(:canceled_at).and_call_original
> --
> You received this message because you are subscribed to the Google Groups "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/0ED673A5-17B0-4C3B-9E0B-9C0E862279BB%40pobox.com.

Jack Royal-Gordon

unread,
Feb 25, 2021, 4:57:44 PM2/25/21
to rs...@googlegroups.com
Thanks, Phil. I was looking for that option (didn’t know the exact syntax) and could not find it. Now that I know what it is I can see that it is reasonably obvious in the documentation.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/CAAk5Ok8Us_5LbZcEEkqBf5v98SHWxOhVJk8mGECMvQmDQMS-_w%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages