calebhearth / formulaic

Simplify form filling with Capybara

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support of accepting object instead passing symbol of type of object

IlkhamGaysin opened this issue · comments

For example we have

fill_form(:user, user_attributes)

and this is used in one spec file.

Lets image we have two or three registration forms through devise or any other authentication plugins and in our system we have two registrable resources for example admins and users or three admins, users, members.

So then instead writing this line

fill_form(:user/:admin/:member, user/admin/member_attributes)

in each scenario for per resource I want to declare it once on my shared_example(https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples) file for example

fill_form(resource, resource_attributes)

By standard way for this purposes I need to create

let(:resource_name) { admin/member/user.class.name.downcase.to_sym }

and pass it to this

fill_form(resource_name, resource_attributes)

Is it possible to add this or it's overhead?

Hi there,

I'm not actually sure what you're asking for here. Scenic doesn't have anything to do with rspec helper methods you're referring to.

It would be clearest in your tests to spell out the resource you're filling. Looking at a line like fill_form(resource_name, resource_attributes) seems to me like it doesn't tell you what's happening, whereas fill_form(:member, attributes_for(:member)) makes it fairly clear what's happening.

But what about DRY? @calebthompson
In case of password reset we have three resources and tests for password reset would be look the same with differences in expectations in the end of spec.

Here some examples:

require "rails_helper"

feature "Password Reset for Admin" do
  let!(:resource) { create :admin }
  let(:resource_name) { resource.class.name.downcase.to_sym }
  let(:attributes_for_resource) { attributes_for :admin }

  background do
    visit sign_in_path
  end

  scenario "Admin resets his password" do
    click_link "Forgot password?"

    fill_form(resource_name, email: resource.email)

    click_button "Send"

    open_email(resource.email)

    visit_in_email("Change my password")

    fill_form(resource_name, :edit, attributes_for_resource.slice(:password, :password_confirmation))

    click_button "Update password"

    expect(current_path).to eq admin_root_path
  end
end

And tests for member/user would be the same except titles and expectations in the end

expect(current_path).to eq admin/member/user_root_path

To eliminate duplication I've separated test actions into the shared_context and spelled in the each scenarios for member/admin/user resource_name, attributes_for_resource

It's about passing an object instead a symbol.

For example it would be a good style to spell this one fill_form(member, attributes) where member and attributes are variables in the test case.

Thanks!