Permalink
Browse files

Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)

Closes #9
  • Loading branch information...
1 parent 799b6a6 commit afea2f56aff595e0f9fc08154e1e9aaefa0997e3 @dchelimsky dchelimsky committed Apr 28, 2010
@@ -0,0 +1,32 @@
+Feature: errors_on
+
+ Scenario: with one validation error
+ Given a file named "spec/models/widget_spec.rb" with:
+ """
+ require "spec_helper"
+
+ class ValidatingWidget < ActiveRecord::Base
+ set_table_name :widgets
+ validates_presence_of :name
+ end
+
+ describe ValidatingWidget do
+ it "fails validation with no name (using error_on)" do
+ ValidatingWidget.new.should have(1).error_on(:name)
+ end
+
+ it "fails validation with no name (using errors_on)" do
+ ValidatingWidget.new.should have(1).errors_on(:name)
+ end
+
+ it "passes validation with a name (using 0)" do
+ ValidatingWidget.new(:name => "liquid nitrogen").should have(0).errors_on(:name)
+ end
+
+ it "passes validation with a name (using :no)" do
+ ValidatingWidget.new(:name => "liquid nitrogen").should have(:no).errors_on(:name)
+ end
+ end
+ """
+ When I run "rspec spec/models/widget_spec.rb"
+ Then I should see "4 examples, 0 failures"
View
@@ -1,4 +1,5 @@
require 'rspec/rails/monkey'
+require 'rspec/rails/extensions'
require 'rspec/rails/adapters'
require 'rspec/rails/transactional_database_support'
require 'rspec/rails/matchers'
@@ -0,0 +1 @@
+require 'rspec/rails/extensions/active_record/base'
@@ -0,0 +1,46 @@
+if defined?(ActiveRecord::Base)
+ module Rspec
+ module Rails
+ module Extensions
+ module ActiveRecord
+ module ClassMethods
+ # :call-seq:
+ # ModelClass.should have(:no).records
+ # ModelClass.should have(1).record
+ # ModelClass.should have(n).records
+ #
+ # Extension to enhance <tt>should have</tt> on AR Model classes
+ def records
+ find(:all)
+ end
+ alias :record :records
+ end
+
+ module InstanceMethods
+ # :call-seq:
+ # model.should have(:no).errors_on(:attribute)
+ # model.should have(1).error_on(:attribute)
+ # model.should have(n).errors_on(:attribute)
+ #
+ # Extension to enhance <tt>should have</tt> on AR Model instances.
+ # Calls model.valid? in order to prepare the object's errors
+ # object.
+ def errors_on(attribute)
+ self.valid?
+ [self.errors[attribute]].flatten.compact
+ end
+ alias :error_on :errors_on
+ end
+ end
+ end
+ end
+ end
+
+ module ActiveRecord #:nodoc:
+ class Base
+ extend Rspec::Rails::Extensions::ActiveRecord::ClassMethods
+ include Rspec::Rails::Extensions::ActiveRecord::InstanceMethods
+ end
+ end
+end
+
@@ -35,3 +35,28 @@ class AssertionFailedError < StandardError
model_klass === actual && actual.new_record?
end
end
+
+require 'rspec/matchers/have'
+
+module Rspec #:nodoc:
+ module Matchers #:nodoc:
+ class Have #:nodoc:
+
+ def failure_message_for_should_with_errors_on_extensions
+ return "expected #{relativities[@relativity]}#{@expected} errors on :#{@args[0]}, got #{@actual}" if @collection_name == :errors_on
+ return "expected #{relativities[@relativity]}#{@expected} error on :#{@args[0]}, got #{@actual}" if @collection_name == :error_on
+ return failure_message_for_should_without_errors_on_extensions
+ end
+ alias_method_chain :failure_message_for_should, :errors_on_extensions
+
+ def description_with_errors_on_extensions
+ return "have #{relativities[@relativity]}#{@expected} errors on :#{@args[0]}" if @collection_name == :errors_on
+ return "have #{relativities[@relativity]}#{@expected} error on :#{@args[0]}" if @collection_name == :error_on
+ return description_without_errors_on_extensions
+ end
+ alias_method_chain :description, :errors_on_extensions
+
+ end
+ end
+end
+
@@ -0,0 +1,38 @@
+require "spec_helper"
+
+describe "error_on" do
+ it "should provide a description including the name of what the error is on" do
+ have(1).error_on(:whatever).description.should == "have 1 error on :whatever"
+ end
+
+ it "should provide a failure message including the number actually given" do
+ lambda {
+ [].should have(1).error_on(:whatever)
+ }.should raise_error("expected 1 error on :whatever, got 0")
+ end
+end
+
+describe "errors_on" do
+ it "should provide a description including the name of what the error is on" do
+ have(2).errors_on(:whatever).description.should == "have 2 errors on :whatever"
+ end
+
+ it "should provide a failure message including the number actually given" do
+ lambda {
+ [1].should have(3).errors_on(:whatever)
+ }.should raise_error("expected 3 errors on :whatever, got 1")
+ end
+end
+
+describe "have something other than error_on or errors_on" do
+ it "has a standard rspec failure message" do
+ lambda {
+ [1,2,3].should have(2).elements
+ }.should raise_error("expected 2 elements, got 3")
+ end
+
+ it "has a standard rspec description" do
+ have(2).elements.description.should == "have 2 elements"
+ end
+end
+

1 comment on commit afea2f5

\o/ :)

Please sign in to comment.