Two days in, Minitest is liberating and inspires innovation.

So, I'm positive the epiphany I just had with Minitest/Capybara was equally possible with Rspec, or Minitest/Spec or Cucumber. But, for whatever reason, I never thought it was possible and never tried. I'm sure I'm moved too greatly by the statement I read promoting Minitest over other testing methods as, "It's just Ruby," but it has allowed me to think, "Hey, this is just Ruby."

Testing is MUCH easier with Minitest and Fixtures than I ever felt it was with Rspec or Cucumber with FactoryGirl. Again, this is probably noob stuff, but, I always felt somewhat daunted by writing the test as I typically had some issue getting everything to properly load and run correctly in test where it really worked when run on the site.

My first test looked like this. Repetitive and long and two days old.

  test 'edit gift certificate from customer profile' do

#assert page.has_title?("#{business.name} Profile Page"), 
#                       "#{business.name} not present."

click_link "#{customer.first_name}"

#assert_equal business_customer_path(business, customer), current_path

click_link "Gift Certificates Purchased"

#assert_equal customer_gift_certificates_path(customer), current_path

#assert page.has_content?(certificate.prices.last.amount),
#                        "Correct amount, #{certificate.prices.last.amount}, not listed."


assert page.has_link?("Edit"),
                     "Edit link not available."

#assert page.has_link?("1"),
#                     "1 link not available."

click_link "Edit"

assert_equal edit_customer_gift_certificate_path(customer, certificate), current_path

assert page.has_title?("Edit this Gift Certificate"),
                      "Edit this Gift Certificate not in the title."

assert page.has_content?("Editing this Gift Certificate"),
                        "Editing this Gift Certificate is not available on the page."


assert page.has_field?('Certificate Amount', with: "100.00"),
                      "Certificate Amount of 100 not showing in a form field."

assert page.has_field?('Certificate Comment', with: "#{certificate.comments.last.comment}"),
                      "Certificate Comment field with '#{certificate.comments.last.comment} not showing."

assert page.has_link?('Back to business page'),
                     "'Back to business page' link is not available."

assert page.has_link?('Back to customer view'),
                     "'Back to customer view' link is not available."

fill_in "Certificate Amount", with: "120"
fill_in "Certificate Comment", with: "Still the first certificate."

click_button "Update Gift certificate"

assert_equal owner_business_path(business.owner, business), current_path

click_link "#{customer.first_name}"

click_link "Gift Certificates Purchased"

assert page.has_content?("$120.00"),
                        "New price of $120 not visible in the table."

click_link "1"

assert_equal customer_gift_certificate_path(customer, certificate), current_path

assert page.has_content?("Certificate Amount: $120.00"),
                        "New Certificate Amount of $120.00 not visible."

assert page.has_content?("Certificate Comments: Still the first certificate."),
                        "New Certificate Comment Still the first certificate not visible."

end

test 'edit gift certificate from main business page' do click_link "Gift Certificates"

assert_equal gift_certificates_business_path(business), current_path

assert page.has_link? "Return to main business page",
                      "'Return to main business page' not available."

assert page.has_link? "Edit",
                      "'Edit' link not available."

click_link 'Edit'

assert_equal edit_customer_gift_certificate_path(customer, certificate), current_path

fill_in "Certificate Amount", with: "120"
fill_in "Certificate Comment", with: "Still the first certificate."

click_button "Update Gift certificate"

assert_equal owner_business_path(business.owner, business), current_path

click_link "Gift Certificates"

assert page.has_content?("$120.00"),
                        "New price of $120 not visible in the table."

click_link "1"

assert_equal customer_gift_certificate_path(customer, certificate), current_path

assert page.has_content?("Certificate Amount: $120.00"),
                        "New Certificate Amount of $120.00 not visible."

assert page.has_content?("Certificate Comments: Still the first certificate."),
                        "New Certificate Comment Still the first certificate not visible."

end

Ugly and repetitive, but works. I get the flow even if it isn't as DSL pretty as Rspec or Cucumber. "It's just Ruby," kept running through my head as I wrote the same line over and over and over essentially.

So, I wrote the following two methods and put them in test/support/shared.rb

def check_content(*args)
  args.each do |arg|
    assert page.has_content?(arg), "Content -- #{arg} not available."
  end
end

def check_links(*args)
  args.each do |arg|
    assert page.has_content?(arg), "Link -- #{arg} not available."
  end
end

I created my own DSL. Cause I'm boss.

Now my test looks like this:

test "show gift certificate from customer show page" do
click_link customer.first_name

click_link "Gift Certificates Purchased"

assert_equal customer_gift_certificates_path(customer), current_path,
             "Expected to be at the customer gift certificates index, but at #{current_path}."

assert page.has_title?("Gift Certificates Purchased by #{customer.name}"),
                       "Title -- Gift Certificates Purchased by #{customer.name} not available."

assert page.has_table?('gift_certificates'),
                     "Table -- gift_certificates table not available."

#Checks all table structure content expected words.   method provided above.
check_content("Gift Certificates Purchased by #{customer.name}", "Certificate Number", "Amount", "Redeemed?", "Date Redeemed", "Purchased By", "Purchased On")

#Checks table data fed by database is accurate
check_content("1", "$100.00", "No", customer.name, customer.created_at.to_date.strftime("%m/%d/%Y"))

#Checks certificate index page for page specific links.
check_links("1", "Edit", "Return to customer profile view", "Return to main business page")

click_link "1"

assert_equal customer_gift_certificate_path(customer, certificate), current_path,
            "Expected to be at customer gift certificate show page, but at #{current_path}."

#checks all table column content on customer show page.
check_content("Certificate Number", "Amount", "Redeemed?", "Date Redeemed", "Purchased By", "Purchased On")

#checks content that should be fed from the database for an unredeemed cert on the show page
check_content("1", "$100.00", "No", customer.name, customer.created_at.to_date.strftime("%m/%d/%Y"))

#checks all links on the customer show page
check_links("Edit", "Return to customer profile view", "Return to main business page")

end

test "show gift certificate from main business page" do click_link "Gift Certificates"

assert_equal gift_certificates_business_path(business), current_path,
            "Expected to be at the business gift certificates index, but at #{current_path}."

assert page.has_title?("All Gift Certificates Sold"), 
                       "Title -- All Gift Certificates Sold not available."

assert page.has_table?("gift_certificates"), 
                      "Table -- gift_certificates table not available."

# checks table structure content on gift_certificates.business index page.
check_content("Certificate Number", "Amount", "Redeemed?", "Date Redeemed", "Purchased By", "Purchased On")

# checks database specific content on gift_certificates.business index page for unredeemed cert.
check_content("1", "$100.00", "No", customer.name, customer.created_at.to_date.strftime("%m/%d/%Y"))

#checks links on gift_certificates.business index page
check_links("1", "Edit", "Redeem", "Return to main business page")

click_link "1"

assert_equal customer_gift_certificate_path(customer, certificate), current_path,
              "Expected to be at customer gift certificate path, but at #{current_path}."

#if here, all assertions match the test in the previous test.

end

I wonder what my tests will look like two days from now :).

Back
paperclip paperclip