Skip to content

Commit

Permalink
Apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Dec 10, 2023
1 parent 6d13813 commit 6ce1526
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 55 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ end
desc "Push docs/cukes to relishapp using the relish-client-gem"
task :relish, :version do |_t, args|
raise "rake relish[VERSION]" unless args[:version]

sh "cp Changelog.md features/"
if `relish versions rspec/rspec-activemodel-mocks`.split.map(&:strip).include? args[:version]
puts "Version #{args[:version]} already exists"
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/additional_cli_steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
Then /^the example(s)? should( all)? pass$/ do |_, _|
Then(/^the example(s)? should( all)? pass$/) do |_, _|
step 'the output should contain "0 failures"'
step 'the exit status should be 0'
end
2 changes: 1 addition & 1 deletion features/step_definitions/model_steps.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
Given /a (\w+) model/ do |model_class_name|
Given(/a (\w+) model/) do |model_class_name|
puts eval(model_class_name)
end
4 changes: 2 additions & 2 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run(cmd)
end

def aruba_path(file_or_dir)
File.expand_path("../../../#{file_or_dir.sub('sample','aruba')}", __FILE__)
File.expand_path("../../../#{file_or_dir.sub('sample', 'aruba')}", __FILE__)
end

def sample_path(file_or_dir)
Expand All @@ -43,7 +43,7 @@ def copy(file_or_dir)
)

Dir['tmp/sample/*'].each do |file_or_dir|
if !(file_or_dir =~ /spec$/)
if file_or_dir !~ /spec$/
write_symlink(file_or_dir)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/active_model/mocks.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'rspec/core'

RSpec::configure do |c|
RSpec.configure do |c|
c.backtrace_exclusion_patterns << /lib\/rspec\/active_model\/mocks/
end

Expand Down
51 changes: 26 additions & 25 deletions lib/rspec/active_model/mocks/mocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
module RSpec::ActiveModel::Mocks
class IllegalDataAccessException < StandardError; end
module Mocks

module ActiveModelInstanceMethods
# Stubs `persisted?` to return false and `id` to return nil
# @return self
Expand Down Expand Up @@ -92,7 +91,7 @@ def association(association_name)
# * A String representing a Class that does not exist
# * A String representing a Class that extends ActiveModel::Naming
# * A Class that extends ActiveModel::Naming
def mock_model(string_or_model_class, stubs = {})
def mock_model(string_or_model_class, stubs={})
if String === string_or_model_class
if Object.const_defined?(string_or_model_class)
model_class = Object.const_get(string_or_model_class)
Expand All @@ -114,22 +113,22 @@ def self.param_delimiter; "-"; end
end

unless model_class.kind_of? ::ActiveModel::Naming
raise ArgumentError.new <<-EOM
raise ArgumentError, <<-EOM
The mock_model method can only accept as its first argument:
* A String representing a Class that does not exist
* A String representing a Class that extends ActiveModel::Naming
* A Class that extends ActiveModel::Naming
It received #{model_class.inspect}
EOM
EOM
end

stubs = {:id => next_id}.merge(stubs)
stubs = {:persisted? => !!stubs[:id],
:destroyed? => false,
:marked_for_destruction? => false,
:valid? => true,
:blank? => false}.merge(stubs)
stubs = { :id => next_id }.merge(stubs)
stubs = { :persisted? => !!stubs[:id],
:destroyed? => false,
:marked_for_destruction? => false,
:valid? => true,
:blank? => false }.merge(stubs)

double("#{model_class.name}_#{stubs[:id]}", stubs).tap do |m|
if model_class.method(:===).owner == Module && !stubs.has_key?(:===)
Expand All @@ -144,11 +143,9 @@ def self.param_delimiter; "-"; end
include ActiveModel::Conversion
include ActiveModel::Validations
end
if defined?(ActiveRecord)
if stubs.values_at(:save, :update_attributes, :update).include?(false)
RSpec::Mocks.allow_message(m.errors, :empty?).and_return(false)
RSpec::Mocks.allow_message(m.errors, :blank?).and_return(false)
end
if defined?(ActiveRecord) && stubs.values_at(:save, :update_attributes, :update).include?(false)
RSpec::Mocks.allow_message(m.errors, :empty?).and_return(false)
RSpec::Mocks.allow_message(m.errors, :blank?).and_return(false)
end

msingleton.__send__(:define_method, :is_a?) do |other|
Expand All @@ -172,12 +169,16 @@ def self.param_delimiter; "-"; end
end unless stubs.has_key?(:has_attribute?)

msingleton.__send__(:define_method, :respond_to?) do |method_name, *args|
include_private = args.first || false
include_private = args.first || false
__model_class_has_column?(method_name) ? true : super(method_name, include_private)
end unless stubs.has_key?(:respond_to?)

msingleton.__send__(:define_method, :method_missing) do |m, *a, &b|
respond_to?(m) ? null_object? ? self : nil : super(m, *a, &b)
if respond_to?(m)
null_object? ? self : nil
else
super(m, *a, &b)
end
end

msingleton.__send__(:define_method, :class) do
Expand Down Expand Up @@ -221,7 +222,8 @@ def new_record?
# Raises an IllegalDataAccessException (stubbed models are not allowed to access the database)
# @raises IllegalDataAccessException
def connection
raise RSpec::ActiveModel::Mocks::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
raise RSpec::ActiveModel::Mocks::IllegalDataAccessException,
"stubbed models are not allowed to access the database"
end
end

Expand Down Expand Up @@ -258,13 +260,13 @@ def stub_model(model_class, stubs={})
if defined?(ActiveRecord) && model_class < ActiveRecord::Base && model_class.primary_key
m.extend ActiveRecordStubExtensions
primary_key = model_class.primary_key.to_sym
stubs = {primary_key => next_id}.merge(stubs)
stubs = {:persisted? => !!stubs[primary_key]}.merge(stubs)
stubs = { primary_key => next_id }.merge(stubs)
stubs = { :persisted? => !!stubs[primary_key] }.merge(stubs)
else
stubs = {:id => next_id}.merge(stubs)
stubs = {:persisted? => !!stubs[:id]}.merge(stubs)
stubs = { :id => next_id }.merge(stubs)
stubs = { :persisted? => !!stubs[:id] }.merge(stubs)
end
stubs = {:blank? => false}.merge(stubs)
stubs = { :blank? => false }.merge(stubs)

stubs.each do |message, return_value|
if m.respond_to?("#{message}=")
Expand All @@ -282,14 +284,13 @@ def stub_model(model_class, stubs={})
end
end

private
private

@@model_id = 1000

def next_id
@@model_id += 1
end

end
end

Expand Down
10 changes: 5 additions & 5 deletions rspec-activemodel-mocks.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Gem::Specification.new do |s|
s.description = "RSpec test doubles for ActiveModel and ActiveRecord"

s.metadata = {
'bug_tracker_uri' => 'https://github.com/rspec/rspec-activemodel-mocks/issues',
'bug_tracker_uri' => 'https://github.com/rspec/rspec-activemodel-mocks/issues',
'documentation_uri' => 'https://rspec.info/documentation/',
'mailing_list_uri' => 'https://groups.google.com/forum/#!forum/rspec',
'source_code_uri' => 'https://github.com/rspec/rspec-activemodel-mocks',
'mailing_list_uri' => 'https://groups.google.com/forum/#!forum/rspec',
'source_code_uri' => 'https://github.com/rspec/rspec-activemodel-mocks'
}

s.files = `git ls-files -- lib/*`.split("\n")
Expand Down Expand Up @@ -46,8 +46,8 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake", "~> 13.0.0"
end

s.add_development_dependency 'cucumber', '>= 1.3'
s.add_development_dependency('activerecord', [">= 3.0"])
s.add_development_dependency 'aruba', '~> 0.4.11'
s.add_development_dependency 'cucumber', '>= 1.3'
s.add_development_dependency 'ZenTest', '~> 4.11.2'
s.add_development_dependency('activerecord', [">= 3.0"])
end
16 changes: 8 additions & 8 deletions spec/rspec/active_model/mocks/mock_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@

describe "with params" do
it "does not mutate its parameters" do
params = {:a => 'b'}
params = { :a => 'b' }
mock_model(MockableModel, params)
expect(params).to eq({:a => 'b'})
expect(params).to eq({ :a => 'b' })
end
end

Expand Down Expand Up @@ -499,9 +499,9 @@ def self.===(_other)
ERR
end
include Test::Unit::Assertions
if defined?(Test::Unit::AutoRunner.need_auto_run = ())
if defined?((Test::Unit::AutoRunner.need_auto_run = ()))
Test::Unit::AutoRunner.need_auto_run = false
elsif defined?(Test::Unit.run = ())
elsif defined?((Test::Unit.run = ()))
Test::Unit.run = false
end
else
Expand All @@ -512,9 +512,9 @@ def self.===(_other)
else
require 'test/unit/assertions'
include Test::Unit::Assertions
if defined?(Test::Unit::AutoRunner.need_auto_run = ())
if defined?((Test::Unit::AutoRunner.need_auto_run = ()))
Test::Unit::AutoRunner.need_auto_run = false
elsif defined?(Test::Unit.run = ())
elsif defined?((Test::Unit.run = ()))
Test::Unit.run = false
end
end
Expand All @@ -524,8 +524,8 @@ def self.===(_other)
include ActiveModel::Lint::Tests

# to_s is to support ruby-1.9
ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
example m.gsub('_',' ') do
ActiveModel::Lint::Tests.public_instance_methods.map {|m| m.to_s}.grep(/^test/).each do |m|
example m.gsub('_', ' ') do
send m
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/rspec/active_model/mocks/stub_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def model_class
it "raises when hitting the db" do
expect do
stub_model(MockableModel).connection
end.to raise_error(RSpec::ActiveModel::Mocks::IllegalDataAccessException, /stubbed models are not allowed to access the database/)
end.to raise_error(RSpec::ActiveModel::Mocks::IllegalDataAccessException,
/stubbed models are not allowed to access the database/)
end

it "increments the id" do
Expand Down
10 changes: 7 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

class RSpec::Core::ExampleGroup
def self.run_all(reporter=nil)
run(reporter || RSpec::Mocks::Mock.new('reporter').as_null_object)
module RSpec
module Core
class ExampleGroup
def self.run_all(reporter=nil)
run(reporter || RSpec::Mocks::Mock.new('reporter').as_null_object)
end
end
end
end

Expand Down
12 changes: 6 additions & 6 deletions spec/support/ar_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@

module Connections
def self.extended(host)
host.connection.execute <<-eosql
host.connection.execute <<-EOSQL
CREATE TABLE #{host.table_name} (
#{host.primary_key} integer PRIMARY KEY AUTOINCREMENT,
associated_model_id integer,
mockable_model_id integer,
nonexistent_model_id integer
)
eosql
EOSQL
end
end

module ConnectionsView
def self.extended(host)
host.connection.execute <<-eosql
host.connection.execute <<-EOSQL
CREATE TABLE some_table (
associated_model_id integer,
mockable_model_id integer,
nonexistent_model_id integer
)
eosql
EOSQL

host.connection.execute <<-eosql
host.connection.execute <<-EOSQL
CREATE VIEW #{host.table_name} AS
select * from some_table;
eosql
EOSQL
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/support/matchers.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true
RSpec::Matchers::define :be_included_in_files_in do |path|
RSpec::Matchers.define :be_included_in_files_in do |path|
match do |mod|
stub_metadata(
:example_group => {:file_path => "#{path}whatever_spec.rb:15"}
:example_group => { :file_path => "#{path}whatever_spec.rb:15" }
)
group = RSpec::Core::ExampleGroup.describe
group.included_modules.include?(mod)
Expand Down

0 comments on commit 6ce1526

Please sign in to comment.