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 On Fri, Feb 26, 2021 at 12:38 AM Jack Royal-Gordon <jac...@pobox.com> wrote: > > 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. > > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/0ED673A5-17B0-4C3B-9E0B-9C0E862279BB%40pobox.com. -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/CAAk5Ok8Us_5LbZcEEkqBf5v98SHWxOhVJk8mGECMvQmDQMS-_w%40mail.gmail.com.