Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added rspec-on-rails-matchers and updated specs to more concise format

  • Loading branch information...
commit bc8c1ffb7b9a1aeff2c5d758b6ce4c9b9cd15ecd 1 parent 3cc8276
@GavinJoyce authored
View
234 lib/tasks/rspec.rake
@@ -1,132 +1,158 @@
-raise "To avoid rake task loading problems: run 'rake clobber' in vendor/plugins/rspec" if File.directory?(File.join(File.dirname(__FILE__), *%w[.. .. vendor plugins rspec pkg]))
-raise "To avoid rake task loading problems: run 'rake clobber' in vendor/plugins/rspec-rails" if File.directory?(File.join(File.dirname(__FILE__), *%w[.. .. vendor plugins rspec-rails pkg]))
-
-# In rails 1.2, plugins aren't available in the path until they're loaded.
-# Check to see if the rspec plugin is installed first and require
-# it if it is. If not, use the gem version.
-rspec_base = File.expand_path(File.dirname(__FILE__) + '/../../vendor/plugins/rspec/lib')
-$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
-require 'spec/rake/spectask'
-
-spec_prereq = File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop
-task :noop do
+rspec_gem_dir = nil
+Dir["#{RAILS_ROOT}/vendor/gems/*"].each do |subdir|
+ rspec_gem_dir = subdir if subdir.gsub("#{RAILS_ROOT}/vendor/gems/","") =~ /^(\w+-)?rspec-(\d+)/ && File.exist?("#{subdir}/lib/spec/rake/spectask.rb")
end
+rspec_plugin_dir = File.expand_path(File.dirname(__FILE__) + '/../../vendor/plugins/rspec')
-task :default => :spec
-task :stats => "spec:statsetup"
+if rspec_gem_dir && (test ?d, rspec_plugin_dir)
+ raise "\n#{'*'*50}\nYou have rspec installed in both vendor/gems and vendor/plugins\nPlease pick one and dispose of the other.\n#{'*'*50}\n\n"
+end
-desc "Run all specs in spec directory (excluding plugin specs)"
-Spec::Rake::SpecTask.new(:spec => spec_prereq) do |t|
- t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
- t.spec_files = FileList['spec/**/*_spec.rb']
+if rspec_gem_dir
+ $LOAD_PATH.unshift("#{rspec_gem_dir}/lib")
+elsif File.exist?(rspec_plugin_dir)
+ $LOAD_PATH.unshift("#{rspec_plugin_dir}/lib")
end
-namespace :spec do
- desc "Run all specs in spec directory with RCov (excluding plugin specs)"
- Spec::Rake::SpecTask.new(:rcov) do |t|
- t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
- t.spec_files = FileList['spec/**/*_spec.rb']
- t.rcov = true
- t.rcov_opts = lambda do
- IO.readlines("#{RAILS_ROOT}/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
- end
- end
-
- desc "Print Specdoc for all specs (excluding plugin specs)"
- Spec::Rake::SpecTask.new(:doc) do |t|
- t.spec_opts = ["--format", "specdoc", "--dry-run"]
- t.spec_files = FileList['spec/**/*_spec.rb']
- end
+begin
+ require 'spec/rake/spectask'
+ Rake.application.instance_variable_get('@tasks').delete('default')
- desc "Print Specdoc for all plugin specs"
- Spec::Rake::SpecTask.new(:plugin_doc) do |t|
- t.spec_opts = ["--format", "specdoc", "--dry-run"]
- t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*')
+ spec_prereq = File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop
+ task :noop do
end
- [:models, :controllers, :views, :helpers, :lib].each do |sub|
- desc "Run the specs under spec/#{sub}"
- Spec::Rake::SpecTask.new(sub => spec_prereq) do |t|
- t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
- t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
- end
- end
-
- desc "Run the specs under vendor/plugins (except RSpec's own)"
- Spec::Rake::SpecTask.new(:plugins => spec_prereq) do |t|
+ task :default => :spec
+ task :stats => "spec:statsetup"
+
+ desc "Run all specs in spec directory (excluding plugin specs)"
+ Spec::Rake::SpecTask.new(:spec => spec_prereq) do |t|
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
- t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*').exclude("vendor/plugins/rspec-rails/*")
+ t.spec_files = FileList['spec/**/*/*_spec.rb']
end
-
- namespace :plugins do
- desc "Runs the examples for rspec_on_rails"
- Spec::Rake::SpecTask.new(:rspec_on_rails) do |t|
+
+ namespace :spec do
+ desc "Run all specs in spec directory with RCov (excluding plugin specs)"
+ Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
- t.spec_files = FileList['vendor/plugins/rspec-rails/spec/**/*_spec.rb']
+ t.spec_files = FileList['spec/**/*/*_spec.rb']
+ t.rcov = true
+ t.rcov_opts = lambda do
+ IO.readlines("#{RAILS_ROOT}/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
+ end
end
- end
- # Setup specs for stats
- task :statsetup do
- require 'code_statistics'
- ::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models')
- ::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views')
- ::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers) if File.exist?('spec/controllers')
- ::STATS_DIRECTORIES << %w(Helper\ specs spec/helpers) if File.exist?('spec/helpers')
- ::STATS_DIRECTORIES << %w(Library\ specs spec/lib) if File.exist?('spec/lib')
- ::CodeStatistics::TEST_TYPES << "Model specs" if File.exist?('spec/models')
- ::CodeStatistics::TEST_TYPES << "View specs" if File.exist?('spec/views')
- ::CodeStatistics::TEST_TYPES << "Controller specs" if File.exist?('spec/controllers')
- ::CodeStatistics::TEST_TYPES << "Helper specs" if File.exist?('spec/helpers')
- ::CodeStatistics::TEST_TYPES << "Library specs" if File.exist?('spec/lib')
- ::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
- end
+ desc "Print Specdoc for all specs (excluding plugin specs)"
+ Spec::Rake::SpecTask.new(:doc) do |t|
+ t.spec_opts = ["--format", "specdoc", "--dry-run"]
+ t.spec_files = FileList['spec/**/*/*_spec.rb']
+ end
- namespace :db do
- namespace :fixtures do
- desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y"
- task :load => :environment do
- require 'active_record/fixtures'
- ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
- (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'spec', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
- Fixtures.create_fixtures('spec/fixtures', File.basename(fixture_file, '.*'))
- end
+ desc "Print Specdoc for all plugin examples"
+ Spec::Rake::SpecTask.new(:plugin_doc) do |t|
+ t.spec_opts = ["--format", "specdoc", "--dry-run"]
+ t.spec_files = FileList['vendor/plugins/**/spec/**/*/*_spec.rb'].exclude('vendor/plugins/rspec/*')
+ end
+
+ [:models, :controllers, :views, :helpers, :lib].each do |sub|
+ desc "Run the code examples in spec/#{sub}"
+ Spec::Rake::SpecTask.new(sub => spec_prereq) do |t|
+ t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
+ t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
end
end
- end
- namespace :server do
- daemonized_server_pid = File.expand_path("spec_server.pid", RAILS_ROOT + "/tmp")
+ desc "Run the code examples in vendor/plugins (except RSpec's own)"
+ Spec::Rake::SpecTask.new(:plugins => spec_prereq) do |t|
+ t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
+ t.spec_files = FileList['vendor/plugins/**/spec/**/*/*_spec.rb'].exclude('vendor/plugins/rspec/*').exclude("vendor/plugins/rspec-rails/*")
+ end
- desc "start spec_server."
- task :start do
- if File.exist?(daemonized_server_pid)
- $stderr.puts "spec_server is already running."
- else
- $stderr.puts "Starting up spec server."
- system("ruby", "script/spec_server", "--daemon", "--pid", daemonized_server_pid)
+ namespace :plugins do
+ desc "Runs the examples for rspec_on_rails"
+ Spec::Rake::SpecTask.new(:rspec_on_rails) do |t|
+ t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
+ t.spec_files = FileList['vendor/plugins/rspec-rails/spec/**/*/*_spec.rb']
end
end
- desc "stop spec_server."
- task :stop do
- unless File.exist?(daemonized_server_pid)
- $stderr.puts "No server running."
- else
- $stderr.puts "Shutting down spec_server."
- system("kill", "-s", "TERM", File.read(daemonized_server_pid).strip) &&
- File.delete(daemonized_server_pid)
+ # Setup specs for stats
+ task :statsetup do
+ require 'code_statistics'
+ ::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models')
+ ::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views')
+ ::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers) if File.exist?('spec/controllers')
+ ::STATS_DIRECTORIES << %w(Helper\ specs spec/helpers) if File.exist?('spec/helpers')
+ ::STATS_DIRECTORIES << %w(Library\ specs spec/lib) if File.exist?('spec/lib')
+ ::CodeStatistics::TEST_TYPES << "Model specs" if File.exist?('spec/models')
+ ::CodeStatistics::TEST_TYPES << "View specs" if File.exist?('spec/views')
+ ::CodeStatistics::TEST_TYPES << "Controller specs" if File.exist?('spec/controllers')
+ ::CodeStatistics::TEST_TYPES << "Helper specs" if File.exist?('spec/helpers')
+ ::CodeStatistics::TEST_TYPES << "Library specs" if File.exist?('spec/lib')
+ ::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
+ end
+
+ namespace :db do
+ namespace :fixtures do
+ desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z."
+ task :load => :environment do
+ ActiveRecord::Base.establish_connection(Rails.env)
+ base_dir = File.join(Rails.root, 'spec', 'fixtures')
+ fixtures_dir = ENV['FIXTURES_DIR'] ? File.join(base_dir, ENV['FIXTURES_DIR']) : base_dir
+
+ (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/).map {|f| File.join(fixtures_dir, f) } : Dir.glob(File.join(fixtures_dir, '*.{yml,csv}'))).each do |fixture_file|
+ Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*'))
+ end
+ end
end
end
- desc "reload spec_server."
- task :restart do
- unless File.exist?(daemonized_server_pid)
- $stderr.puts "No server running."
- else
- $stderr.puts "Reloading down spec_server."
- system("kill", "-s", "USR2", File.read(daemonized_server_pid).strip)
+ namespace :server do
+ daemonized_server_pid = File.expand_path("spec_server.pid", RAILS_ROOT + "/tmp")
+
+ desc "start spec_server."
+ task :start do
+ if File.exist?(daemonized_server_pid)
+ $stderr.puts "spec_server is already running."
+ else
+ $stderr.puts "Starting up spec server."
+ system("ruby", "script/spec_server", "--daemon", "--pid", daemonized_server_pid)
+ end
+ end
+
+ desc "stop spec_server."
+ task :stop do
+ unless File.exist?(daemonized_server_pid)
+ $stderr.puts "No server running."
+ else
+ $stderr.puts "Shutting down spec_server."
+ system("kill", "-s", "TERM", File.read(daemonized_server_pid).strip) &&
+ File.delete(daemonized_server_pid)
+ end
+ end
+
+ desc "reload spec_server."
+ task :restart do
+ unless File.exist?(daemonized_server_pid)
+ $stderr.puts "No server running."
+ else
+ $stderr.puts "Reloading down spec_server."
+ system("kill", "-s", "USR2", File.read(daemonized_server_pid).strip)
+ end
end
end
end
+rescue MissingSourceFile
+ # if rspec-rails is a configured gem, this will output helpful material and exit ...
+ require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")
+
+ # ... otherwise, do this:
+ raise <<-MSG
+
+ You have rspec rake tasks installed in
+ #{__FILE__},
+ but rspec can not be found in vendor/gems, vendor/plugins or on the system.
+
+MSG
end
+
View
25 script/spec_server
@@ -13,17 +13,21 @@ module Spec
def run(argv, stderr, stdout)
$stdout = stdout
$stderr = stderr
-
- base = ActiveRecord::Base
- def base.clear_reloadable_connections!
- active_connections.each do |name, conn|
- if conn.requires_reloading?
- conn.disconnect!
- active_connections.delete(name)
+
+ unless ActiveRecord::Base.respond_to?(:clear_reloadable_connections!)
+ base = ActiveRecord::Base
+ def base.clear_reloadable_connections!
+ active_connections.each do |name, conn|
+ if conn.requires_reloading?
+ conn.disconnect!
+ active_connections.delete(name)
+ end
end
end
end
+ ActiveRecord::Base.clear_reloadable_connections!
+
if ActionController.const_defined?(:Dispatcher)
dispatcher = ::ActionController::Dispatcher.new($stdout)
dispatcher.cleanup_application
@@ -35,8 +39,13 @@ module Spec
if Object.const_defined?(:Fixtures) && Fixtures.respond_to?(:reset_cache)
Fixtures.reset_cache
end
+
+ if ::ActiveSupport.const_defined?(:Dependencies)
+ ::ActiveSupport::Dependencies.mechanism = :load
+ else
+ ::Dependencies.mechanism = :load
+ end
- ::Dependencies.mechanism = :load
require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
load File.dirname(__FILE__) + '/../spec/spec_helper.rb'
View
52 spec/models/job_spec.rb
@@ -28,54 +28,20 @@
it "should be valid with valid attributes" do
Factory(:job).should be_valid
end
-
- it "should ensure that a company is provided" do
- @job = Factory.build(:job, :company => nil)
- @job.should_not be_valid
- @job.errors.on(:company).should_not be_nil
- end
-
- it "should ensure that an email is provided" do
- @job = Factory.build(:job, :email => nil)
- @job.should_not be_valid
- @job.errors.on(:email).should_not be_nil
- end
-
+
+ it { should validate_presence_of(:company) }
+ it { should validate_presence_of(:email) }
+ it { should validate_presence_of(:type_id) }
+ it { should validate_presence_of(:location_id) }
+ it { should validate_presence_of(:title) }
+ it { should validate_presence_of(:description) }
+ it { should validate_presence_of(:how_to_apply) }
+
it "should ensure that email is a valid email" do
@job = Factory.build(:job, :email => 'invalid@email')
@job.should_not be_valid
@job.errors.on(:email).should_not be_nil
end
-
- it "should ensure that a type_id is provided" do
- @job = Factory.build(:job, :type_id => nil)
- @job.should_not be_valid
- @job.errors.on(:type_id).should_not be_nil
- end
-
- it "should ensure that a location_id is provided" do
- @job = Factory.build(:job, :location_id => nil)
- @job.should_not be_valid
- @job.errors.on(:location_id).should_not be_nil
- end
-
- it "should ensure that a title is provided" do
- @job = Factory.build(:job, :title => nil)
- @job.should_not be_valid
- @job.errors.on(:title).should_not be_nil
- end
-
- it "should ensure that a description is provided" do
- @job = Factory.build(:job, :description => nil)
- @job.should_not be_valid
- @job.errors.on(:description).should_not be_nil
- end
-
- it "should ensure that a how_to_apply is provided" do
- @job = Factory.build(:job, :how_to_apply => nil)
- @job.should_not be_valid
- @job.errors.on(:how_to_apply).should_not be_nil
- end
end
describe "searching" do
View
6 spec/models/location_spec.rb
@@ -7,10 +7,6 @@
Factory.build(:location).should be_valid
end
- it "should ensure that a name is provided" do
- location = Factory.build(:location, :name => nil)
- location.should_not be_valid
- location.errors.on(:name).should_not be_nil
- end
+ it { should validate_presence_of(:name) }
end
end
View
10 spec/models/type_spec.rb
@@ -2,17 +2,11 @@
require File.dirname(__FILE__) + '/../factory'
describe Type do
- #it { should validate_presence_of(:name) } #NOTE: GJ: why is this not working with rspec-rails 1.1.12??
-
describe "validations" do
it "should be valid with valid attributes" do
Factory.build(:type).should be_valid
end
-
- it "should ensure that a name is provided" do
- type = Factory.build(:type, :name => nil)
- type.should_not be_valid
- type.errors.on(:name).should_not be_nil
- end
+
+ it { should validate_presence_of(:name) }
end
end
View
10 vendor/plugins/rspec-on-rails-matchers/CHANGELOG
@@ -0,0 +1,10 @@
+Change Log
+==========
+
+Trunk
+-----
+
+* 2008/03/02 - Added have_form_putting_to(url_or_path) - Patch by unknown google code submitter
+* 2008/03/02 - Added should observe (Luke Melia)
+* 2008/03/02 - Patched validates_length_of to use within to be consistent with Rails (Matt Pelletier)
+* 2007/01/03 - Initial Public Release
View
20 vendor/plugins/rspec-on-rails-matchers/MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 The Plugin Development Team
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
187 vendor/plugins/rspec-on-rails-matchers/README
@@ -0,0 +1,187 @@
+rspec-on-rails-matchers
+=======================
+
+Setup
+------
+
+Dependencies:
+-------------
+
+ * rspec
+ * rspec_on_rails
+
+Overview
+--------
+
+Adds the following RSpec matchers:
+
+ * Associations:
+ Verify that the association has been defined. (doesn't verify that the association works!)
+
+ object.should have_many(:association)
+ example: @post.should have_many(:comments)
+ TM snippet: [mshm + tab] (Model Should Have Many)
+
+ object.should belong_to(:association)
+ example: @comment.should belong_to(:post)
+ TM snippet: [msbt + tab]
+
+ object.should have_one(:association)
+ user.should have_one(:social_security_number)
+ TM snippet: [msho + tab]
+
+ object.should have_and_belong_to_many(:association)
+ project.should have_and_belong_to_many(:categories)
+ TM snippet: [mshabtm + tab]
+
+
+ * Validations:
+ Verify that a validation has been defined. (doesn't test the validation itself)
+
+ object.should validate_presence_of(:attribute)
+ TM snippet: [msvp + tab]
+
+ object.should validate_confirmation_of(:attribute)
+ TM snippet: [msvc + tab]
+
+ object.should validate_uniqueness_of(:attribute)
+ TM snippet: [msvu + tab]
+
+ object.should validate_length_of(:attribute, :within => 5..10)
+ object.should validate_length_of(:attribute, :is => 5)
+ TM snippet: [msvl + tab]
+
+ * Observers:
+ Verify that the observer is observing a class. (doesn't verify that the observation works)
+
+ object.should observe(:model)
+ example: GroupObserver.should observe(Group)
+
+ * Views:
+ Verifies that the views contains some tags.
+
+ response.should have_form_posting_to(url_or_path)
+ TM snippet: [hfpt + tab]
+
+ response.should have_form_putting_to(url_or_path)
+
+ response.should have_text_field_for(:attribute)
+ TM snippet: [htff + tab]
+
+ response.should have_label_for(:attribute)
+ TM snippet: [hlf + tab]
+
+ response.should have_password_field_for(:attribute)
+ TM snippet: [hpff + tab]
+
+ response.should have_checkbox_for(:attribute)
+ TM snippet: [hcf + tab]
+
+ response.should have_submit_button
+ TM snippet: [hsb + tab]
+
+ response.should have_link_to(url_or_path, "optional_text")
+ TM snippet: [hlt + tab]
+
+ * nested view tests:
+ for instance:
+
+ response.should have_form_posting_to(url_or_path) do
+ with_text_field_for(:attribute)
+ end
+
+ with_text_field_for(:attribute)
+ TM snippet: [wtff + tab]
+
+ with_label_for(:attribute)
+ TM snippet: [wlf + tab]
+
+ with_password_field_for(:attribute)
+ TM snippet: [wpff + tab]
+
+ with_checkbox_for(:attribute)
+ TM snippet: [wcf + tab]
+
+ with_submit_button
+ TM snippet: [wsb + tab]
+
+ with_link_to(url_or_path, "optional_text")
+ TM snippet: [wlt + tab]
+
+Usage:
+------
+
+In your view spec:
+
+ it "should render new form" do
+ render "/users/new.html.erb"
+
+ response.should have_form_posting_to(users_path) do
+ with_text_field_for(:user_name)
+ with_text_area_for(:user_address)
+ with_text_field_for(:user_login)
+ with_text_field_for(:user_email)
+ with_submit_button
+ end
+ end
+
+In your model spec:
+
+ describe User do
+ before(:each) do
+ @user = User.new
+ end
+
+ it "should have many posts" do
+ @user.should have_many(:posts)
+ end
+
+ it "should belong to a group" do
+ @user.should belong_to(:group)
+ end
+
+ it do
+ @user.should validate_presence_of(:email)
+ end
+
+ it do
+ @user.should validate_uniqueness_of(:email)
+ end
+
+ it do
+ @user.should validate_uniqueness_of(:login)
+ end
+
+ it do
+ @user.should validate_presence_of(:login)
+ end
+
+ it do
+ @user.should validate_presence_of(:name)
+ end
+
+ it do
+ @user.should validate_length_of(:password, :between => 4..40)
+ end
+
+ it do
+ @user.should validate_confirmation_of(:password)
+ end
+
+ end
+
+Core Contributors
+-----------------
+
+ * Josh Knowles <joshknowles@gmail.com>
+ * Bryan Helmkamp <bryan@brynary.com>
+ * Matt Aimonetti <mattaimonetti@gmail.com>
+
+Contributors
+-------------
+
+ * ckknight
+ * Matt Pelletier
+ * Luke Melia
+
+Copyright (c) 2008 The Plugin Development Team, released under the MIT license
View
4 vendor/plugins/rspec-on-rails-matchers/TODO
@@ -0,0 +1,4 @@
+To Do
+=====
+
+* Update README to include instructions on installing from github
View
5 vendor/plugins/rspec-on-rails-matchers/init.rb
@@ -0,0 +1,5 @@
+require 'spec/rails/matchers/observers'
+require 'spec/rails/matchers/associations'
+require 'spec/rails/matchers/validations'
+require 'spec/rails/matchers/views'
+require 'spec/rails/matchers/observers'
View
33 vendor/plugins/rspec-on-rails-matchers/lib/spec/rails/matchers/associations.rb
@@ -0,0 +1,33 @@
+module Spec
+ module Rails
+ module Matchers
+ def belong_to(association)
+ return simple_matcher("model to belong to #{association}") do |model|
+ model = model.class if model.is_a? ActiveRecord::Base
+ model.reflect_on_all_associations(:belongs_to).find { |a| a.name == association }
+ end
+ end
+
+ def have_many(association)
+ return simple_matcher("model to have many #{association}") do |model|
+ model = model.class if model.is_a? ActiveRecord::Base
+ model.reflect_on_all_associations(:has_many).find { |a| a.name == association }
+ end
+ end
+
+ def have_one(association)
+ return simple_matcher("model to have one #{association}") do |model|
+ model = model.class if model.is_a? ActiveRecord::Base
+ model.reflect_on_all_associations(:has_one).find { |a| a.name == association }
+ end
+ end
+
+ def have_and_belong_to_many(association)
+ return simple_matcher("model to have and belong to many #{association}") do |model|
+ model = model.class if model.is_a? ActiveRecord::Base
+ model.reflect_on_all_associations(:has_and_belongs_to_many).find { |a| a.name == association }
+ end
+ end
+ end
+ end
+end
View
34 vendor/plugins/rspec-on-rails-matchers/lib/spec/rails/matchers/observers.rb
@@ -0,0 +1,34 @@
+module Spec
+ module Rails
+ module Matchers
+
+ class Observe
+ def initialize(expected_model_class)
+ @expected_model_class = expected_model_class
+ end
+
+ def matches?(observer)
+ @observer = observer
+ if @observer.is_a?(ActiveRecord::Observer)
+ @observer = @observer.class
+ end
+ @observed_classes = observer.observed_classes.flatten
+ @observed_classes.include?(@expected_model_class)
+ end
+
+ def failure_message
+ return "expected #{@observer.name} to observe #{@expected_model_class.name}, but it was not included in [#{@observed_classes.map(&:name).join(', ')}]"
+ end
+
+ def description
+ "observer to be observing #{@expected_model_class.name}"
+ end
+ end
+
+ def observe(expected_model_class)
+ Observe.new(expected_model_class)
+ end
+
+ end
+ end
+end
View
56 vendor/plugins/rspec-on-rails-matchers/lib/spec/rails/matchers/validations.rb
@@ -0,0 +1,56 @@
+module Spec
+ module Rails
+ module Matchers
+ def validate_presence_of(attribute)
+ return simple_matcher("model to validate the presence of #{attribute}") do |model|
+ model.send("#{attribute}=", nil)
+ !model.valid? && model.errors.invalid?(attribute)
+ end
+ end
+
+ def validate_length_of(attribute, options)
+ if options.has_key? :within
+ min = options[:within].first
+ max = options[:within].last
+ elsif options.has_key? :is
+ min = options[:is]
+ max = min
+ elsif options.has_key? :minimum
+ min = options[:minimum]
+ elsif options.has_key? :maximum
+ max = options[:maximum]
+ end
+
+ return simple_matcher("model to validate the length of #{attribute} within #{min || 0} and #{max || 'Infinity'}") do |model|
+ invalid = false
+ if !min.nil? && min >= 1
+ model.send("#{attribute}=", 'a' * (min - 1))
+
+ invalid = !model.valid? && model.errors.invalid?(attribute)
+ end
+
+ if !max.nil?
+ model.send("#{attribute}=", 'a' * (max + 1))
+
+ invalid ||= !model.valid? && model.errors.invalid?(attribute)
+ end
+ invalid
+ end
+ end
+
+ def validate_uniqueness_of(attribute)
+ return simple_matcher("model to validate the uniqueness of #{attribute}") do |model|
+ model.class.stub!(:find).and_return(true)
+ !model.valid? && model.errors.invalid?(attribute)
+ end
+ end
+
+ def validate_confirmation_of(attribute)
+ return simple_matcher("model to validate the confirmation of #{attribute}") do |model|
+ model.send("#{attribute}_confirmation=", 'asdf')
+ !model.valid? && model.errors.invalid?(attribute)
+ end
+ end
+ end
+ end
+end
View
102 vendor/plugins/rspec-on-rails-matchers/lib/spec/rails/matchers/views.rb
@@ -0,0 +1,102 @@
+module Spec
+ module Rails
+ module Matchers
+ def have_form_posting_to(url_or_path)
+ return simple_matcher("have a form submitting via POST to '#{url_or_path}'") do |response|
+ have_tag("form[method=post][action=#{url_or_path}]").matches?(response)
+ end
+ end
+
+ def have_form_puting_to(url_or_path, id)
+ return simple_matcher("have a form submitting via PUT to '#{url_or_path}/#{id}'") do |response|
+ have_tag("form[method=post][action=#{url_or_path}/#{id}]").matches?(response)
+ have_tag("input[name=_method][type=hidden][value=put]").matches?(response)
+ end
+ end
+
+ def have_label_for(attribute, text)
+ return simple_matcher("have a label for '#{attribute}' with value of '#{text}'") do |response|
+ have_tag("label[for=#{attribute}]").matches?(response)
+ end
+ end
+
+ def with_label_for(attribute, text)
+ return simple_matcher("have a label for '#{attribute}' with value of '#{text}'") do |response|
+ with_tag("label[for=#{attribute}]").matches?(response)
+ end
+ end
+
+ def have_text_field_for(attribute)
+ return simple_matcher("have a text field for '#{attribute}'") do |response|
+ have_tag("input##{attribute}[type=text]").matches?(response)
+ end
+ end
+
+ def with_text_field_for(attribute)
+ return simple_matcher("with a text field for '#{attribute}'") do |response|
+ with_tag("input##{attribute}[type=text]").matches?(response)
+ end
+ end
+
+ def have_text_area_for(attribute)
+ return simple_matcher("have a text field for '#{attribute}'") do |response|
+ have_tag("textarea##{attribute}[type=text]").matches?(response)
+ end
+ end
+
+ def with_text_area_for(attribute)
+ return simple_matcher("have a text field for '#{attribute}'") do |response|
+ with_tag("textarea##{attribute}[type=text]").matches?(response)
+ end
+ end
+
+ def have_password_field_for(attribute)
+ return simple_matcher("have a password field for '#{attribute}'") do |response|
+ have_tag("input##{attribute}[type=password]").matches?(response)
+ end
+ end
+
+ def with_password_field_for(attribute)
+ return simple_matcher("have a password field for '#{attribute}'") do |response|
+ with_tag("input##{attribute}[type=password]").matches?(response)
+ end
+ end
+
+ def have_checkbox_for(attribute)
+ return simple_matcher("have a checkbox for '#{attribute}'") do |response|
+ have_tag("input##{attribute}[type=checkbox]").matches?(response)
+ end
+ end
+
+ def with_checkbox_for(attribute)
+ return simple_matcher("have a checkbox for '#{attribute}'") do |response|
+ with_tag("input##{attribute}[type=checkbox]").matches?(response)
+ end
+ end
+
+ def have_submit_button
+ return simple_matcher("have a submit button") do |response|
+ have_tag("input[type=submit]").matches?(response)
+ end
+ end
+
+ def with_submit_button
+ return simple_matcher("have a submit button") do |response|
+ with_tag("input[type=submit]").matches?(response)
+ end
+ end
+
+ def have_link_to(url_or_path, text = nil)
+ return simple_matcher("have a link to '#{url_or_path}'") do |response|
+ have_tag("a[href=#{url_or_path}]", text).matches?(response)
+ end
+ end
+
+ def with_link_to(url_or_path, text = nil)
+ return simple_matcher("have a link to '#{url_or_path}'") do |response|
+ with_tag("a[href=#{url_or_path}]", text).matches?(response)
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.