I'm writing a rspec/capybara test that ensures input fields in a form display the correct validations.
I'm concerned that my code is not very readable. How can I refactor this to make sure that it's readable?
describe "#page" do
context "form validation" do
1.upto(4) do |index|
it "throws correct validation error when #{5-index} field(s) is (are) empty" do
login(page_path)
fill_in_form(index)
find('.button').trigger(:click)
expect(all('.form-error').count).to eq 5-index
all('.form-error')[-1...index-5].each do |error|
expect(error.text).to eq "#{@inputs[5-index][:error_message]} is required"
end
end
end
end
end
def fill_in_form(number_of_fields)
(0).upto(number_of_fields-1) do |i|
fill_in(@inputs[i][:name], :with => (@inputs[i][:value]))
end
end
def login(path)
visit(path)
# redirected to login
acceptance_login(@user)
visit(path)
# input fields
@inputs = [
{name: 'first_name', error_message: "First name is not valid", value: "John"},
{name: 'last_name', error_message: "Last name is not valid", value: "Doe"},
{name: 'company', error_message: "Company name is not valid", value: "My company"},
{name: 'role', error_message: "Role is not valid", value: "Developer"},
{name: 'phone', error_message: "Phone number is not valid", value: "(800) 492-1111"}
]
end
Aucun commentaire:
Enregistrer un commentaire