Ruby on rails 需要在功能规范(RSpec)中设置方法的帮助吗
我的程序代码按预期工作,但我需要帮助使用RSpec(3.1.0)和FactoryGirl在功能规范中设置方法。我的编辑规范的相关部分如下所示:Ruby on rails 需要在功能规范(RSpec)中设置方法的帮助吗,ruby-on-rails,testing,rspec,capybara,Ruby On Rails,Testing,Rspec,Capybara,我的程序代码按预期工作,但我需要帮助使用RSpec(3.1.0)和FactoryGirl在功能规范中设置方法。我的编辑规范的相关部分如下所示: require 'rails_helper' describe "Editing appointments" do let!(:appointment) { FactoryGirl.create(:appointment) } def update_appointment(options={}) options[:date] ||= "20
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”方法和/或分配新值造成的。我不知道如何最好地解决这个问题。我将感谢任何帮助 我试图成功完成这个规范。 请查看我在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/appointment\u spec.rb
.
Finished in 0.23874 seconds (files took 3.56 seconds to load)
1 example, 0 failures
我认为你没有选择权。只需在
更新约会
方法中选择2018年12月15日。您应该使用select
方法中的选项。如下面(选项[:year])to_i,:from=>'appointment\u date\u 1i')`感谢您的回复,但我不理解您的建议。请修改代码,告诉我您认为我应该如何更改我的更新预约方法。我该如何在我的方法中“选择2018年12月15日”呢?感谢您帮助我修复我的功能规范。您对我的设置的改进非常有效。我感谢你的帮助!我添加了其他测试,现在一切都按预期通过了。稍后我将尝试重构此规范以改进它。我正在学习Rails和测试。。。这很有帮助,我很高兴听到。祝你过得愉快:)