所以我正在尝试为此方法编写规范:
class Student
has_many :payments
def payments_by_year
payments.group_by { |payment| payment.payment_date.year }
end
end
我的规格看起来像这样:
RSpec.describe Student do
let!(:student) { create :student, first_name: 'Bruce', last_name: 'Wayne' }
let!(:student2) { create :student, first_name: 'Peter', last_name: 'Parker' }
let!(:payment) { create :payment, student: student, payment_date: Date.new(2016,9,1) }
let!(:payment2) { create :payment, student: student, payment_date: Date.new(2016,8,1) }
let!(:payment3) { create :payment, student: student, payment_date: Date.new(2015,7,1) }
describe '#payments_by_year' do
describe 'returns hash of payments with years as keys' do
subject { student.payments_by_year }
it { is_expected.to eq ####SOMETHING SHOULD BE HERE}
end
end
end
我的问题是我不知道传递给:eq
方法的是什么,因为我无法例外地访问let变量:
`payment` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). (RSpec::Core::ExampleGroup::WrongScopeError)
所以我无法硬编码正确的结果。如何在describe
块中访问这些变量?