Created
August 25, 2010 08:48
-
-
Save solnic/549130 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| module Spec | |
| module Matchers | |
| RSpec::Matchers.define :have_errors_on do |field| | |
| chain :with_message do |message| | |
| @message = message | |
| end | |
| match do |model| | |
| model.valid? | |
| @has_error = has_error?(model, field) | |
| if @message | |
| @has_error && model.errors[field].include?(@message) | |
| else | |
| @has_error | |
| end | |
| end | |
| failure_message_for_should do |model| | |
| msg = "#{model.class} should have an error on field #{field.inspect}" | |
| if @has_error && @message | |
| msg << " with a message #{@message.inspect}" | |
| msg << " while it is #{model.errors[field].inspect}" | |
| end | |
| msg | |
| end | |
| failure_message_for_should_not do |model| | |
| "#{model.class} should not have an error on field #{field.inspect}" | |
| end | |
| description do | |
| end | |
| def has_error?(model, field) | |
| !model.errors[field].blank? | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or 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 File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | |
| describe User do | |
| describe "validation of " do | |
| subject { Factory.build(:invalid_user) } | |
| describe "email" do | |
| describe "when has less then 6 chars" do | |
| before(:each) { subject.email = 'f@b' } | |
| it { should have_errors_on(:email).with_message("Email must be between 6 and 64 characters long") } | |
| end | |
| describe "when has more then 64 chars" do | |
| before(:each) { subject.email = 'this.is.a.very.long.email.that.has.more.then.64.ch@rs.for.sure.com' } | |
| it { should have_errors_on(:email).with_message("Email must be between 6 and 64 characters long") } | |
| end | |
| describe "when has more then 6 and less then 64 chars" do | |
| before(:each) { subject.email = '[email protected]' } | |
| it { should_not have_errors_on(:email).with_message("Email must be between 6 and 64 characters long") } | |
| end | |
| end | |
| describe "first_name" do | |
| it { should have_errors_on(:first_name).with_message("First name must be between 3 and 50 characters long") } | |
| end | |
| describe "last_name" do | |
| it { should have_errors_on(:last_name).with_message("Last name must be between 3 and 50 characters long") } | |
| end | |
| describe "degree" do | |
| it { should have_errors_on(:degree).with_message("Degree is not included in the list") } | |
| end | |
| describe "password" do | |
| it { should have_errors_on(:password).with_message("Password must be between 6 and 20 characters long") } | |
| end | |
| end | |
| describe "default state" do | |
| before :all do | |
| @user = User.new | |
| end | |
| it "sets degree to mgr" do | |
| @user.degree.should eql('mgr') | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment