我这里有两个规格:
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我不明白的是,如果我在第二个规范expect(house).to be_valid中替换为is_expected.to be_valid,那么我的测试就失败了:
失败:
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这一切为什么要发生?提前谢谢!
发布于 2018-11-28 15:35:02
is_expected被简单地定义为expect(subject),是为您使用rspec期望值及其更新的基于预期的语法而设计的。
https://relishapp.com/rspec/rspec-core/docs/subject/one-liner-syntax
由于被测试的对象是house而不是subject,我假设subject没有初始化,因此被设置为defaul (described_class.new)。is_expected调用这个默认主题的期望。
要使用is_expected,初始化subject:
describe House do
subject { House.new(...) } # or build :house if you use FactoryBot
# ...
endhttps://stackoverflow.com/questions/53522899
复制相似问题