Permalink
Browse files

better Rails 3 compatibility

  • Loading branch information...
1 parent ff7006e commit 436b56527692c0a9096f68ca0a4772cd65a77df0 @mislav committed Jun 18, 2011
Showing with 86 additions and 68 deletions.
  1. +2 −1 Gemfile
  2. +30 −28 Gemfile.lock
  3. +18 −3 README.markdown
  4. +0 −7 lib/rspec-rails-mocha.rb
  5. +35 −18 lib/rspec/rails/mocha.rb
  6. +1 −11 spec/mocha_spec.rb
View
@@ -2,4 +2,5 @@ source :rubygems
gem 'rspec-rails'
gem 'activesupport'
-gem 'mocha'
+gem 'activemodel'
+gem 'mocha'
View
@@ -2,59 +2,61 @@ GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
- actionpack (3.0.3)
- activemodel (= 3.0.3)
- activesupport (= 3.0.3)
+ actionpack (3.0.9)
+ activemodel (= 3.0.9)
+ activesupport (= 3.0.9)
builder (~> 2.1.2)
erubis (~> 2.6.6)
- i18n (~> 0.4)
+ i18n (~> 0.5.0)
rack (~> 1.2.1)
- rack-mount (~> 0.6.13)
- rack-test (~> 0.5.6)
+ rack-mount (~> 0.6.14)
+ rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
- activemodel (3.0.3)
- activesupport (= 3.0.3)
+ activemodel (3.0.9)
+ activesupport (= 3.0.9)
builder (~> 2.1.2)
- i18n (~> 0.4)
- activesupport (3.0.3)
+ i18n (~> 0.5.0)
+ activesupport (3.0.9)
builder (2.1.2)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
- mocha (0.9.10)
- rake
- rack (1.2.1)
- rack-mount (0.6.13)
+ mocha (0.9.12)
+ rack (1.2.3)
+ rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
- railties (3.0.3)
- actionpack (= 3.0.3)
- activesupport (= 3.0.3)
+ railties (3.0.9)
+ actionpack (= 3.0.9)
+ activesupport (= 3.0.9)
rake (>= 0.8.7)
+ rdoc (~> 3.4)
thor (~> 0.14.4)
- rake (0.8.7)
- rspec (2.4.0)
- rspec-core (~> 2.4.0)
- rspec-expectations (~> 2.4.0)
- rspec-mocks (~> 2.4.0)
- rspec-core (2.4.0)
- rspec-expectations (2.4.0)
+ rake (0.9.2)
+ rdoc (3.6.1)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
- rspec-mocks (2.4.0)
- rspec-rails (2.4.1)
+ rspec-mocks (2.6.0)
+ rspec-rails (2.6.1)
actionpack (~> 3.0)
activesupport (~> 3.0)
railties (~> 3.0)
- rspec (~> 2.4.0)
+ rspec (~> 2.6.0)
thor (0.14.6)
- tzinfo (0.3.24)
+ tzinfo (0.3.28)
PLATFORMS
ruby
DEPENDENCIES
+ activemodel
activesupport
mocha
rspec-rails
View
@@ -3,17 +3,28 @@ rspec-rails Mocha extension
Functionality of `mock_model` and `stub_model` from rspec-rails for those using Mocha.
-Installation for **Rails 3**:
+Installation for **Rails 3** (rspec-rails v2.6 and above):
# Gemfile
+ group :development, :test do
+ gem 'rspec-rails', '~> 2.4'
+ end
+
group :test do
- gem 'rspec-rails-mocha', '~> 0.3.0'
+ gem 'rspec-rails-mocha', '~> 0.3.1', :require => false
end
-For **Rails 2**, use the '0.2.x' version:
+ # spec/spec_helper.rb
+ require 'rspec/rails'
+ require 'rspec/rails/mocha'
+
+For **Rails 2** (rspec-rails v1.3.x), use the '0.2.x' branch:
gem 'rspec-rails-mocha', '~> 0.2.1'
+Then, for any version of Rails, ensure that your "spec_helper.rb" file
+**doesn't contain** `config.mock_with :rspec`.
+
Usage:
describe "Mocha plugin" do
@@ -24,3 +35,7 @@ Usage:
person.id.should == 66
end
end
+
+You should be able to use `mock_model` and `stub_model` in the same way
+you use them when mocking with rspec instead of mocha. [Here are the
+docs](http://relishapp.com/rspec/rspec-rails/v/2-6/dir/mocks/mock-model).
View
@@ -1,7 +0,0 @@
-require 'rspec/core'
-require 'rspec/rails/mocha'
-
-RSpec.configure do |config|
- config.mock_with :mocha
- config.include RSpec::Rails::Mocha
-end
View
@@ -1,12 +1,18 @@
+require 'active_support/core_ext'
require 'active_model'
module RSpec
module Rails
+
+ unless defined? IllegalDataAccessException
+ class IllegalDataAccessException < StandardError; end
+ end
+
module Mocha
+
module ActiveModelInstanceMethods
def as_new_record
- self.stubs(:persisted?) { false }
- self.stubs(:id) { nil }
+ self.stubs(:persisted? => false, :id => nil)
self
end
@@ -21,8 +27,11 @@ def respond_to?(message, include_private=false)
module ActiveRecordInstanceMethods
def destroy
- self.stubs(:persisted?) { false }
- self.stubs(:id) { nil }
+ self.stubs(:persisted? => false, :id => nil)
+ end
+
+ def [](key)
+ send(key)
end
def new_record?
@@ -71,21 +80,23 @@ def mock_model(string_or_model_class, stubs = {})
EOM
end
- stubs = stubs.reverse_merge(:id => next_id)
+ stubs = stubs.reverse_merge \
+ :id => next_id,
+ :destroyed? => false,
+ :marked_for_destruction? => false,
+ :blank? => false
+
stubs = stubs.reverse_merge(:persisted? => !!stubs[:id])
- stubs = stubs.reverse_merge(:destroyed? => false)
- stubs = stubs.reverse_merge(:marked_for_destruction? => false)
- stubs = stubs.reverse_merge(:errors => stub("errors", :count => 0, :[] => [], :empty? => true))
stub("#{model_class.name}_#{stubs[:id]}", stubs).tap do |m|
m.extend ActiveModelInstanceMethods
- model_class.__send__ :include, ActiveModel::Conversion
- model_class.__send__ :include, ActiveModel::Validations
+ m.singleton_class.__send__ :include, ActiveModel::Conversion
+ m.singleton_class.__send__ :include, ActiveModel::Validations
if defined?(ActiveRecord)
m.extend ActiveRecordInstanceMethods
[:save, :update_attributes].each do |key|
if stubs[key] == false
- m.errors.stubs(:empty?) { false }
+ m.errors.stubs(:empty? => false)
end
end
end
@@ -108,21 +119,20 @@ def class
def to_s
"#{model_class.name}_#{to_param}"
end
-
- def to_key
- [id]
- end
CODE
yield m if block_given?
end
end
module ActiveModelStubExtensions
def as_new_record
- self.stubs(:persisted?) { false }
- self.stubs(:id) { nil }
+ self.stubs(:persisted? => false, :id => nil)
self
end
+
+ def persisted?
+ true
+ end
end
module ActiveRecordStubExtensions
@@ -136,7 +146,7 @@ def new_record?
end
def connection
- raise RSpec::Rails::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
+ raise IllegalDataAccessException.new("stubbed models are not allowed to access the database")
end
end
@@ -187,6 +197,7 @@ def stub_model(model_class, stubs={})
stubs = stubs.reverse_merge(:id => next_id)
stubs = stubs.reverse_merge(:persisted? => !!stubs[:id])
end
+ stubs = stubs.reverse_merge(:blank? => false)
stubs.each do |k,v|
m.__send__("#{k}=", stubs.delete(k)) if m.respond_to?("#{k}=")
end
@@ -202,6 +213,12 @@ def stub_model(model_class, stubs={})
def next_id
@@model_id += 1
end
+
end
end
end
+
+RSpec.configure do |config|
+ config.mock_with :mocha
+ config.include RSpec::Rails::Mocha
+end
View
@@ -1,5 +1,5 @@
require 'rspec'
-require 'rspec-rails-mocha'
+require 'rspec/rails/mocha'
# Simple stub for ActiveRecord::Base
module ActiveRecord
@@ -16,9 +16,6 @@ module RSpec::Rails
class IllegalDataAccessException < StandardError; end
end
-require 'active_support/core_ext/hash/reverse_merge'
-require 'active_support/core_ext/object'
-
class Person < ActiveRecord::Base
attr_accessor :id, :name
@@ -59,13 +56,6 @@ class Hacker < Person
person.id.should == 66
end
- it "should use given ID in to_key" do
- personA = create(Person, :id => 66)
- personA.to_key.should == [66]
- personB = create(Person)
- personB.to_key.first.should >= 1000
- end
-
it "should mock a record with properties" do
hacker = create(Hacker, :name => "Mislav", :skillz => 1337)
hacker.name.should == "Mislav"

0 comments on commit 436b565

Please sign in to comment.