1

我有两个模型:LessonBooking 和 CustomerRequests

支付课程预订后,相关的客户请求将被标记为已预订。注意:LessonBooking 不直接通过 id 关联,但客户在付款时输入他们的电子邮件,此电子邮件用于检查现有的客户请求,如果找到,则标记为“已预订”。这是lesson_booking.rb中的相关代码:

event :payment_received do
  transition :form_started => :paid
end
after_transition :form_started => :paid do |booking|
  email = booking.teaching_relationship.student.account.email
  customer_request = CustomerRequest.find_by_email(email)
  unless customer_request.nil?
    customer_request.book
  end
end

CustomerRequest 模型本身也有一个状态机,它有一个事件“book”,如下所示:

event :book do
  transition [:new, :opened, :awaiting_response] => :booked
end

现在我无法通过一个规范来测试 LessonBooking 从“form_started”到“paid”的转换以及 CustomerRequest 从“new”到“booked”的以下转换。

这是我写的规范:

context 'when there is an associated customer request' do
  before :each do
    @student = create(:student)
    relationship = create(:teaching_relationship, student: @student)
    @new_booking = create(:lesson_booking, teaching_relationship: relationship)
    @customer_request = create(:customer_request, student: @student, email: @student.account.email )
  end

  it "it changes the state of the customer request" do
    @new_booking.payment_received
    expect(@customer_request.state).to eq 'booked'
  end
end

结尾

我的测试总是失败并出现以下注释:

expected: "booked"
got: "new"

我知道一般的测试,我会很感激任何帮助。

4

0 回答 0