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

时间:2015-01-22 02:02:48

标签: ruby-on-rails testing rspec capybara

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

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”方法和/或分配新值引起的。我不知道如何最好地解决问题。我很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

我试图成功这个规范。 请在spec文件中查看我在这里评论。 您选择了 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