I have two specs here:
it 'is not an included preferred gender' do
house.preferred_gender = 3
is_expected.not_to be_valid
end
it 'is an included preferred gender' do
house.preferred_gender = 2
expect(house).to be_valid
end
What I don't understand is that if I replace in my second spec expect(house).to be_valid
for is_expected.to be_valid
, then my test fails:
Failures:
1) House preferred gender is an included preferred gender
Failure/Error: is_expected.to be_valid
expected #<House id: nil, rent: nil, deposit: nil, description: nil, preferred_gender: nil, created_at: nil, updated_at: nil, available_at: nil, user_id: nil, lease_length: nil, built_in: nil> to be valid, but got errors: User must exist, Rent can't be blank, Rent is not a number, Preferred gender can't be blank, Preferred gender is not included in the list, Available at can't be blank
# ./spec/models/house_spec.rb:94:in `block (3 levels) in <main>'
Finished in 16.27 seconds (files took 3.02 seconds to load)
52 examples, 1 failure
Why does this happen? Thanks a lot in advance!
is_expected
is defined simply as expect(subject) and is designed for when you are using rspec-expectations with its newer expect-based syntax.
https://relishapp.com/rspec/rspec-core/docs/subject/one-liner-syntax
As the object under test is house
not subject
, I assume that subject
is not initialized, therefore set to defaul (described_class.new
). is_expected
calls expectation on this default subject.
To use is_expected
, initialize subject:
describe House do
subject { House.new(...) } # or build :house if you use FactoryBot
# ...
end