目標:嘗試提交空的註冊表單由於驗證錯誤而失敗。期望在場景中呈現爲單線(方法)。錯誤正在根據其位置進行非常精確的檢查。我不想使用經典計數,直到剩下唯一的方法。'未定義的本地變量或方法'用於期望導出到助手的方法
def cant_be_blank_error_is_displayed(number_of_times = 1)
expect(page).to have_content t('errors.messages.blank'), count: number_of_times
end
require 'rails_helper'
require 'email_helper'
feature 'register organisation', type: :feature, js: true do
let!(:register_page) { RegisterPage.new }
let!(:login_page) { LoginPage.new }
let!(:organisation) { create :organisation, name: 'organisation_name' }
let!(:owner) { create :user, email: '[email protected]', role: :owner }
let(:form) { register_page.register_form }
before { register_page.load }
context 'creation fails because' do
scenario 'mandatory fields were left empty' do
register_page.submit_empty_form
all_mandatory_fields_cant_be_blank_error_displayed
# expect(form).to have_content mail_blank_or_taken_error
end
scenario (...)
end
end
方法:
1.定義在頁面的特定錯誤作爲元素
class RegisterPage < SitePrism::Page
set_url "/sign_up"
section :register_form, :xpath, "//*[@id='new_user']" do
element :email, :xpath, "//*[@id='user_email']"
(...) # other input fields
# binding.pry confirms they appear on the page
element :mail_blank_or_taken_error, :xpath, "//*[@id='new_user']/div[2]/span"
element :blank_password_error, :xpath, "//*[@id='new_user']/div[4]/span"
element :password_too_short_error, :xpath, "//*[@id='new_user']/div[2]/div[3]/p"
element :mismatch_error, :xpath, "//*[@id='new_user']/div[2]/div[4]/span"
element :blank_name_error, :xpath, "//*[@id='new_user']/div[2]/div[5]/span"
element :name_taken_error, :xpath, "//*[@id='new_user']/div[2]/div[5]"
element :wrong_format_error, :xpath, "//*[@id='new_user']/div[7]/span
(...)
end
end
2.創建error_alerts.rb
輔助存儲在一個方法期望
def all_mandatory_fields_cant_be_blank_error_displayed
[
mail_blank_or_taken_error,
blank_password_error,
signs_limit_error,
blank_name_error,
blank_adress_line_error,
blank_city_error,
blank_country_error,
blank_zipcode_error,
blank_phone_number_error,
blank_website_error,
].each do |validation_errors|
expect(form).to have_content validation_errors
end
end
障礙: 錯誤是默認的軌道的,所以他們都沒有定義爲翻譯,我在另一個規範使用相同的約定,一切都準確無誤地運行。
例如:
def invalid_email_or_password_error_displayed
expect(login_page).to have_content t("devise.failure.invalid", authentication_keys: "Email")
end
失敗:我收到以下錯誤。
1#我的方式 - 同樣的問題發生從|validation_errors|
1) register organisation creation fails because mandatory fields were left empty
Failure/Error: mail_blank_or_taken_error,
NameError:
undefined local variable or method `mail_blank_or_taken_error' for #<RSpec::ExampleGroups::RegisterOrganisation::CreationFailsBecause:0x007fe56b336370>
# ./spec/support/error_alerts.rb:63:in `all_mandatory_fields_cant_be_blank_error_displayed'
# ./spec/features/organisation_registration_spec.rb:16:in `block (3 levels) in <top (required)>'
2#經典每個對象預計
1) register organisation creation fails because mandatory fields were left empty
Failure/Error: expect(form).to have_content
mail_blank_or_taken_error
NameError:
undefined local variable or method `mail_blank_or_taken_error' for # <RSpec::ExampleGroups::RegisterOrganisation: :CreationFailsBecause:0x007ff02e7b9348>#./spec/features/organisation_registration_spec.rb:17:in `block (3 levels) in <top (required)>'
問:是我的方式進行一些修改或之後技術上是可以實現唯一的解決辦法是使用經典的計數方法?循環可以迭代頁面的元素嗎?這是一個類似使用的例子,雖然期望是字符串,循環本身包含在spec的場景中,而不是在助手中被導出到外部方法。
scenario "is successful" do
all_ministry_memberships_page.create_new(ministry, cabinet, minister)
[
t("flash.actions.create.notice", resource_name: "Ministry membership"),
"Ministry " + ministry.name,
"Cabinet " + cabinet.name,
"Minister " + minister.name,
"Start Date " + (cabinet.appointment_date + 2.days).strftime("%Y-%m-%d"),
"Created At " + ministry_membership.created_at.strftime("%B %0e, %Y %H:%M"),
"Updated At " + ministry_membership.updated_at.strftime("%B %0e, %Y %H:%M"),
].each do |form_input|
expect(form).to have_content form_input
end
end
這不完全我想達到的效果,因爲'mail_black_or_taken_error'是在'register_page'定義的錯誤元件的名稱。不是文字出現在網站上,所以我得到這個錯誤: '1)註冊機構創建失敗,因爲必填字段留empty' '故障/錯誤:預期(表格)。爲了希望能夠找到文本have_content VALIDATION_ERRORS 「mail_blank_or_taken_error」,在「請仔細閱讀下面的問題:*電子郵件不能blank' 這1關: '[ 「EMAIL不能爲空」, blank_password_error, ] .' 但它不't檢查文本的位置 – Zelka
@Zelka:好吧,難道你不知道如何將i18n鍵變成翻譯後的字符串嗎?(提示:看看你的工作示例) –
創建翻譯並不是預期的效果。而且,這更耗時。 ;)我找到了解決我的問題。將張貼它。 – Zelka