需要帮助在功能规范中设置方法 (RSpec)

Need help setting up a method in a feature spec (RSpec)

我的程序代码按预期工作,但我需要帮助使用 RSpec (3.1.0) & FactoryGirl 在功能规范中设置方法。我的编辑规范的相关部分如下:

require 'rails_helper'

describe "Editing appointments" do

  let!(:appointment) { FactoryGirl.create(:appointment) }

def update_appointment(options={})
  options[:date] ||= "2018-01-02"
  options[:starts_at] ||= "08:00:00"
  options[:ends_at] ||= "10:00:00"
  options[:member_id] ||= 1
  options[:trainer_id] ||= 1

  appointment = options[:appointment]

  visit "/appointments"
  within "#appointment_#{appointment.id}" do
  click_link "Edit"
  #save_and_open_page    
end

  select('2018', :from => 'appointment_date_1i')
  select('December', :from => 'appointment_date_2i')
  select('15', :from => 'appointment_date_3i')
  select('10', :from => 'appointment_starts_at_4i')
  select('00', :from => 'appointment_starts_at_5i')
  select('12', :from => 'appointment_ends_at_4i')
  select('00', :from => 'appointment_ends_at_5i') 

  click_button "Update Appointment"

end

it "updates an appointment successfully with corrected information" do

  update_appointment appointment: appointment,  

    date: '2020-06-11',
    starts_at: '08:00:00',
    ends_at: '10:00:00'                      

  appointment.reload

  expect(page).to have_content ("Appointment was successfully updated.")
  expect(appointment.date).to eq("2020-06-11")    
  expect(appointment.starts_at).to eq("08:00:00")
  expect(appointment.ends_at).to eq("10:00:00")

  end
end

当我 运行 规范时,RSpec 生成以下错误消息:

1) Editing appointments updates an appointment successfully with corrected information
 Failure/Error: expect(appointment.date).to eq("2020-06-11")

   expected: "2020-06-11"
        got: Sat, 15 Dec 2018

   (compared using ==)

   Diff:
   @@ -1,2 +1,2 @@
   -"2020-06-11"
   +Sat, 15 Dec 2018

似乎没有用新值更新约会。我假设这是由我如何尝试调用 "update_appointment" 方法 and/or 分配新值引起的。我不知道如何最好地解决这个问题。如果有任何帮助,我将不胜感激!

我试图通过此规范。 请查看 我在此处评论 在规范文件中。 您选择了 2018 年 12 月 15 日。这就是为什么结果也是 2018 年 12 月 15 日

先决条件:

rails g scaffold Appointment title date:date starts_at:time ends_at:time

规格文件:

require 'rails_helper'

describe "Appointments", :type => :feature do
  describe "Editing appointments" do

    let!(:appointment) { FactoryGirl.create(:appointment) }

    def update_appointment(options={})
      options[:date]       ||= Date.parse("2018-01-02")
      options[:starts_at]  ||= Time.parse("08:00:00")
      options[:ends_at]    ||= Time.parse("10:00:00")
      options[:member_id]  ||= 1
      options[:trainer_id] ||= 1

      appointment = options[:appointment]

      visit "/appointments"

      within "#appointment_#{appointment.id}" do
        click_link "Edit"
        #save_and_open_page
      end

      select(options[:date].year,  :from => 'appointment_date_1i')
      # I commented here!
      select(options[:date].strftime("%B"), :from => 'appointment_date_2i')
      select(options[:date].strftime("%d"),   :from => 'appointment_date_3i')
      select(options[:starts_at].strftime("%H"), :from => 'appointment_starts_at_4i')
      select(options[:starts_at].strftime("%M"),  :from => 'appointment_starts_at_5i')
      select(options[:ends_at].strftime("%H"), :from => 'appointment_ends_at_4i')
      select(options[:ends_at].strftime("%M"),  :from => 'appointment_ends_at_5i')

      click_button "Update Appointment"
    end

    it "updates an appointment successfully with corrected information" do

      update_appointment appointment: appointment,
                         date:      Date.parse("2020-06-11"),
                         starts_at: Time.parse("08:00:00"),
                         ends_at:   Time.parse("10:00:00")

      appointment.reload

      expect(page).to have_content ("Appointment was successfully updated.")
      expect(appointment.date.to_s(:db)).to                 eq("2020-06-11")
      expect(appointment.starts_at.strftime("%H:%M:%S")).to eq("08:00:00")
      expect(appointment.ends_at.strftime("%H:%M:%S")).to   eq("10:00:00")

    end
  end
end

而且,运行 rspec spec/features/appointments_spec.rb

.

Finished in 0.23874 seconds (files took 3.56 seconds to load)
1 example, 0 failures