Created
November 21, 2013 15:49
-
-
Save tuomasj/7584071 to your computer and use it in GitHub Desktop.
Testing Activation Emails with Capybara, RSpec and Custom Matcher. I wrote blog post which is here: http://tuom.as/2013/11/20/testing-activation-emails-with-capybara-rspec-and-custom-matcher.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe "Activation Email" do | |
it "confirms the email when user clicks the confirmation link on activation email" do | |
register_new_user("john.smith@example.com", "adobe password", "adobe password") | |
activation_token = User.last.activation_code | |
expect( open_last_email.body).to have_link_with_endswith(activation_token) | |
visit activation_path(activation_token) | |
expect(page).to have_content( "Your email has been confirmed.") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/features/support/have_link_endswith.rb | |
module Capybara | |
module RSpecMatchers | |
class HaveLinkEndswith < Matcher | |
attr_reader :matching_value | |
def initialize(*args) | |
@matching_value = args.first | |
end | |
def matches?(actual) | |
@actual = wrap(actual) | |
@actual.has_selector?( :css, "a[href$='#{matching_value}']") | |
end | |
def does_not_match?(actual) | |
@actual = wrap(actual) | |
@actual.has_no_selector?( :css, "a[href$='#{matching_value}']") | |
end | |
def failure_message_for_should | |
"expected there be a link which href ends with #{matching_value.inspect} in #{@actual.text}" | |
end | |
def failure_message_for_should_not | |
"expected there not be a link which href ends with #{matching_value.inspect} in #{@actual.text}" | |
end | |
def description | |
"link with href that ends with #{format(content)}" | |
end | |
end | |
def have_link_that_endswith(*args) | |
HaveLinkEndswith.new(*args) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thank you very much for this, it has helped me solve a problem.
I have had to make some minor changes to your code to make it work with the latest versions of the gems used. - specifically:
failure_message_for_should
is nowfailure_message
failure_message_for_should_not
is nowfailure_message_when_negated
open_last_email.body
is nowopen_last_email.default_part_body
And I've also changed the method and class names of the matcher to
have_link_that_ends_with
andHaveLinkEndsWith
- but that's a personal preference thing.You can see all the changes I've made here: https://gist.github.com/LimeBlast/edcfe156c5d719eaea1e
Again, thank you.