Skip to content

Commit 5996959

Browse files
committed
Merge pull request #19 from rspec/read_attribute
Fix mocking belongs_to associations in Rails 4.2+
2 parents 55d72f6 + 0198586 commit 5996959

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

.travis.yml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
script: "bin/rake --trace 2>&1"
2+
sudo: false
23
bundler_args: "--binstubs --without documentation"
34
before_install:
4-
- gem update --system 2.1.11
5+
- gem update --system
56
- gem --version
67
- gem install bundler
78
rvm:
89
- 1.8.7
910
- 1.9.2
1011
- 1.9.3
11-
- 2.0.0
12-
- 2.1.0
12+
- 2.0
13+
- 2.1
14+
- 2.2
1315
env:
1416
- RAILS_VERSION=master
17+
- RAILS_VERSION=4-2-stable
18+
- RAILS_VERSION=4-1-stable
1519
- RAILS_VERSION=4-0-stable
16-
- RAILS_VERSION=4.0.2
1720
- RAILS_VERSION=3-2-stable
18-
- RAILS_VERSION=3.2.16
1921
- RAILS_VERSION=3.1.12
2022
- RAILS_VERSION=3.0.20
2123
matrix:
2224
exclude:
25+
# MRI 2.2 is not supported on a few versions
26+
- rvm: 2.2
27+
env: RAILS_VERSION=3.1.12
28+
- rvm: 2.2
29+
env: RAILS_VERSION=3.0.20
2330
# 3.0.x is not supported on MRI 2.0.0
2431
- rvm: 2.0.0
2532
env: RAILS_VERSION=3.0.20
@@ -31,8 +38,16 @@ matrix:
3138
env: RAILS_VERSION=master
3239
- rvm: 1.9.2
3340
env: RAILS_VERSION=master
41+
- rvm: 1.8.7
42+
env: RAILS_VERSION=4-2-stable
43+
- rvm: 1.8.7
44+
env: RAILS_VERSION=4-1-stable
3445
- rvm: 1.8.7
3546
env: RAILS_VERSION=4-0-stable
47+
- rvm: 1.9.2
48+
env: RAILS_VERSION=4-2-stable
49+
- rvm: 1.9.2
50+
env: RAILS_VERSION=4-1-stable
3651
- rvm: 1.9.2
3752
env: RAILS_VERSION=4-0-stable
3853
- rvm: 1.8.7
@@ -41,3 +56,8 @@ matrix:
4156
env: RAILS_VERSION=4.0.2
4257
allow_failures:
4358
- env: RAILS_VERSION=master
59+
60+
branches:
61+
only:
62+
- master
63+
- /^\d+-\d+-maintenance$/

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ group :documentation do
2020
gem 'github-markup', '1.0.0'
2121
end
2222

23-
case version = ENV.fetch('RAILS_VERSION', '4.0.2')
23+
case version = ENV.fetch('RAILS_VERSION', '4.2.4')
2424
when /\Amaster\z/, /stable\z/
2525
gem "activerecord", :github => "rails/rails", :branch => version
2626
gem "activemodel", :github => "rails/rails", :branch => version
@@ -31,4 +31,6 @@ else
3131
gem "activesupport", version
3232
end
3333

34+
gem "test-unit", '~> 3' if (version >= '3.2.22' || version == '3-2-stable') && version < '4.0.0'
35+
3436
gem "i18n", '< 0.7.0' if RUBY_VERSION < '1.9.3'

lib/rspec/active_model/mocks/mocks.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def [](key)
5252
send(key)
5353
end
5454

55+
# Rails>4.2 uses _read_attribute internally, as an optimized
56+
# alternative to record['id']
57+
alias_method :_read_attribute, :[]
58+
5559
# Returns the opposite of `persisted?`
5660
def new_record?
5761
!persisted?

spec/rspec/active_model/mocks/mock_model_spec.rb

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,55 @@
414414
include Minitest::Assertions
415415
include MinitestAssertion
416416
rescue LoadError
417-
require 'test/unit/assertions'
418-
include Test::Unit::Assertions
417+
if RUBY_VERSION >= '2.2.0'
418+
# Minitest / TestUnit has been removed from ruby core. However, we are
419+
# on an old Rails version and must load the appropriate gem
420+
version = ENV.fetch('RAILS_VERSION', '4.2.4')
421+
if version >= '4.0.0'
422+
# ActiveSupport 4.0.x has the minitest '~> 4.2' gem as a dependency
423+
# This gem has no `lib/minitest.rb` file.
424+
gem 'minitest' if defined?(Kernel.gem)
425+
require 'minitest/unit'
426+
include MiniTest::Assertions
427+
elsif version >= '3.2.22' || version == '3-2-stable'
428+
begin
429+
# Test::Unit "helpfully" sets up autoload for its `AutoRunner`.
430+
# While we do not reference it directly, when we load the `TestCase`
431+
# classes from AS (ActiveSupport), AS kindly references `AutoRunner`
432+
# for everyone.
433+
#
434+
# To handle this we need to pre-emptively load 'test/unit' and make
435+
# sure the version installed has `AutoRunner` (the 3.x line does to
436+
# date). If so, we turn the auto runner off.
437+
require 'test/unit'
438+
require 'test/unit/assertions'
439+
rescue LoadError => e
440+
raise LoadError, <<-ERR.squeeze, e.backtrace
441+
Ruby 2.2+ has removed test/unit from the core library. Rails
442+
requires this as a dependency. Please add test-unit gem to your
443+
Gemfile: `gem 'test-unit', '~> 3.0'` (#{e.message})"
444+
ERR
445+
end
446+
include Test::Unit::Assertions
447+
if defined?(Test::Unit::AutoRunner.need_auto_run = ())
448+
Test::Unit::AutoRunner.need_auto_run = false
449+
elsif defined?(Test::Unit.run = ())
450+
Test::Unit.run = false
451+
end
452+
else
453+
raise LoadError, <<-ERR.squeeze
454+
Ruby 2.2+ doesn't support this version of Rails #{version}
455+
ERR
456+
end
457+
else
458+
require 'test/unit/assertions'
459+
include Test::Unit::Assertions
460+
if defined?(Test::Unit::AutoRunner.need_auto_run = ())
461+
Test::Unit::AutoRunner.need_auto_run = false
462+
elsif defined?(Test::Unit.run = ())
463+
Test::Unit.run = false
464+
end
465+
end
419466
end
420467

421468
require 'active_model/lint'

0 commit comments

Comments
 (0)