Ruby on rails 使用Rspec和Rails进行转换后的测试
我有两种型号:LessonBooking和CustomerRequests 一旦支付了LessonBooking,关联的CustomerRequests将被标记为booked。注意:LessonBooking不通过id直接关联,但客户在付款时会输入他们的电子邮件,此电子邮件用于检查现有的客户请求,如果发现这些请求,则会标记为booked。以下是第_booking.rb课中的相关代码:Ruby on rails 使用Rspec和Rails进行转换后的测试,ruby-on-rails,rspec,state-machine,Ruby On Rails,Rspec,State Machine,我有两种型号:LessonBooking和CustomerRequests 一旦支付了LessonBooking,关联的CustomerRequests将被标记为booked。注意:LessonBooking不通过id直接关联,但客户在付款时会输入他们的电子邮件,此电子邮件用于检查现有的客户请求,如果发现这些请求,则会标记为booked。以下是第_booking.rb课中的相关代码: event :payment_received do transition :form_started =&
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模型本身也有一个状态机,它有一个事件簿作为
如图所示:
event :book do
transition [:new, :opened, :awaiting_response] => :booked
end
现在,我无法通过一个测试LessonBooking从form_开始到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"
我知道一般的测试,我会感谢任何帮助。将其抽象为一个方法,检查是否收到,然后在isolationok中测试该方法,所以你的意思是我应该在转换后为代码编写一个方法:form\u开始:paid,对吗?如果是这样,我将如何检查/测试它是否被接收?类似于:转换后:form\u start=>:paid,do::method\u name