rspec / rspec-its

`its` for RSpec 3 extracted from rspec-core 2.x
MIT License
260 stars 37 forks source link

its([:attr]) has the proper value, its(:attr) does not #53

Closed bbtdev closed 6 years ago

bbtdev commented 6 years ago

`require 'rails_helper'

RSpec.describe Session, type: :model do subject { create :session } # or subject! no dif

its(:expire) { should eq(1) } # value nil its([:expire]) { should eq(1) } # proper value end`

If it is not a bug, my apologies, maybe it's my lack of knowledge

myronmarston commented 6 years ago

its([:attr]) has the proper value, its(:attr) does not

These are intended to do different things, and them getting the same value is purely coincidental. its([:expire]) is the equivalent of calling subject[:expire]; its(:attr) is the equivalent of calling subject.expire.

Try this:

require 'rails_helper'

RSpec.describe Session, type: :model do
  it 'returns the expected value from `.expire`' do
    expect(create(:session).expire).to eq 1
  end

  it 'returns the expected value from `[:expire]`' do
    expect(create(:session)[:expire]).to eq 1
  end
end

Given the results you said you got, I expect the first one will fail (e.g. "expected 1, got nil"), which would indicate that's simply how your session object behaves.

In general, its isn't recommended by the RSpec maintainers. It hides what's going on.