Skip to content

Commit

Permalink
Merge pull request #64 from akestner/ak-rspec-matcher
Browse files Browse the repository at this point in the history
Adds RSpec Matcher
  • Loading branch information
Vladimir Krylov authored Dec 21, 2017
2 parents 7be4b0d + 36144bb commit b170db5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/rspec_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
RSpec::Matchers.define :validate_url_of do |attribute|
match do
actual = subject.is_a?(Class) ? subject.new : subject
actual.send(:"#{attribute}=", "htp://invalidurl")
expect(actual).to be_invalid
@expected_message ||= I18n.t("errors.messages.url")
expect(actual.errors.messages[attribute.to_sym]).to include(@expected_message)
end
chain :with_message do |message|
@expected_message = message
end
end
2 changes: 1 addition & 1 deletion spec/resources/user_with_custom_scheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class UserWithCustomScheme

attr_accessor :homepage

validates :homepage, url { schemes: ['ftp'] }
validates :homepage, url: { schemes: ['ftp'] }
end
13 changes: 13 additions & 0 deletions spec/rspec_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "spec_helper"

if defined?(ActiveModel)
require_relative "../lib/rspec_matcher"

describe "RSpec matcher" do
subject { User }

it "should ensure that attributes are validated" do
should validate_url_of(:homepage)
end
end
end

0 comments on commit b170db5

Please sign in to comment.