Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove mock_model and stub_model from rspec-rails

Some tests still fail without them as I have not yet figured out
a good way to fix them yet. View scaffold generators are also
currently broken.

rspec/rspec-rails#894
  • Loading branch information...
commit 29c175832f55d180473f35c22d1b88245a828917 1 parent bf8b1e9
@thomas-holmes thomas-holmes authored alindeman committed
View
147 features/mocks/mock_model.feature
@@ -1,147 +0,0 @@
-Feature: mock_model
-
- The `mock_model` method generates a test double that acts like an instance of
- `ActiveModel`. This is different from the `stub_model` method which generates
- an instance of a real model class.
-
- The benefit of `mock_model` over `stub_model` is that it is a true double, so
- the examples are not dependent on the behavior (or mis-behavior), or even the
- existence of any other code. If you're working on a controller spec and you
- need a model that doesn't exist, you can pass `mock_model` a string and the
- generated object will act as though its an instance of the class named by
- that string.
-
- Scenario: passing a string that represents a non-existent constant
- Given a file named "spec/models/car_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe "mock_model('Car') with no Car constant in existence" do
- it "generates a constant" do
- expect(Object.const_defined?(:Car)).to be_falsey
- mock_model("Car")
- expect(Object.const_defined?(:Car)).to be_truthy
- end
-
- describe "generates an object that ..." do
- it "returns the correct name" do
- car = mock_model("Car")
- expect(car.class.name).to eq("Car")
- end
-
- it "says it is a Car" do
- car = mock_model("Car")
- expect(car).to be_a(Car)
- end
- end
- end
- """
- When I run `rspec spec/models/car_spec.rb`
- Then the examples should all pass
-
- Scenario: passing a string that represents an existing constant
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe Widget do
- it "uses the existing constant" do
- widget = mock_model("Widget")
- expect(widget).to be_a(Widget)
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
-
- Scenario: passing a class that does not extend ActiveModel::Naming
- Given a file named "spec/models/string_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe String do
- it "raises" do
- expect { mock_model(String) }.to raise_exception
- end
- end
- """
- When I run `rspec spec/models/string_spec.rb`
- Then the examples should all pass
-
- Scenario: passing an Active Record constant
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe Widget do
- let(:widget) { mock_model(Widget) }
-
- it "is valid by default" do
- expect(widget).to be_valid
- end
-
- it "is not a new record by default" do
- expect(widget).not_to be_new_record
- end
-
- it "can be converted to a new record" do
- expect(widget.as_new_record).to be_new_record
- end
-
- it "sets :id to nil upon destroy" do
- widget.destroy
- expect(widget.id).to be_nil
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
-
- Scenario: passing an Active Record constant with method stubs
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe "mock_model(Widget) with stubs" do
- let(:widget) do
- mock_model Widget, :foo => "bar",
- :save => true,
- :update_attributes => false
- end
-
- it "supports stubs for methods that don't exist in ActiveModel or ActiveRecord" do
- expect(widget.foo).to eq("bar")
- end
-
- it "supports stubs for methods that do exist" do
- expect(widget.save).to eq(true)
- expect(widget.update_attributes).to be_falsey
- end
-
- describe "#errors" do
- context "with update_attributes => false" do
- it "is not empty" do
- expect(widget.errors).not_to be_empty
- end
- end
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
-
- Scenario: mock_model outside rails
- Given a file named "mock_model_outside_rails_spec.rb" with:
- """ruby
- require 'rspec/rails/mocks'
-
- describe "Foo" do
- it "is mockable" do
- foo = mock_model("Foo")
- expect(foo.id).to eq(1001)
- expect(foo.to_param).to eq("1001")
- end
- end
- """
- When I run `rspec mock_model_outside_rails_spec.rb`
- Then the examples should all pass
View
58 features/mocks/stub_model.feature
@@ -1,58 +0,0 @@
-Feature: stub_model
-
- The stub_model method generates an instance of a Active Model model.
-
- While you can use stub_model in any example (model, view, controller,
- helper), it is especially useful in view examples, which are inherently
- more state-based than interaction-based.
-
- Scenario: passing an Active Record constant with a hash of stubs
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe "stub_model(Widget) with a hash of stubs" do
- let(:widget) do
- stub_model Widget, :id => 5, :random_attribute => true
- end
-
- it "stubs :id" do
- expect(widget.id).to eql(5)
- end
-
- it "stubs :random_attribute" do
- expect(widget.random_attribute).to be_truthy
- end
-
- it "returns false for new_record? if :id is set" do
- expect(widget).not_to be_new_record
- end
-
- it "can be converted to a new record" do
- widget.as_new_record
- expect(widget).to be_new_record
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
-
- Scenario: passing an Active Record constant with a block of stubs
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe "stub_model(Widget) with a block of stubs" do
- let(:widget) do
- stub_model Widget do |widget|
- widget.id = 5
- end
- end
-
- it "stubs :id" do
- expect(widget.id).to eql(5)
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
View
1  lib/rspec/rails.rb
@@ -12,6 +12,5 @@
require 'rspec/rails/adapters'
require 'rspec/rails/matchers'
require 'rspec/rails/fixture_support'
-require 'rspec/rails/mocks'
require 'rspec/rails/example'
require 'rspec/rails/vendor/capybara'
View
272 lib/rspec/rails/mocks.rb
@@ -1,272 +0,0 @@
-require 'active_support'
-require 'active_support/deprecation'
-require 'active_support/core_ext'
-require 'active_model'
-
-module RSpec
- module Rails
-
- class IllegalDataAccessException < StandardError; end
-
- module Mocks
-
- module ActiveModelInstanceMethods
- # Stubs `persisted?` to return false and `id` to return nil
- # @return self
- def as_new_record
- RSpec::Mocks.allow_message(self, :persisted?).and_return(false)
- RSpec::Mocks.allow_message(self, :id).and_return(nil)
- self
- end
-
- # Returns true by default. Override with a stub.
- def persisted?
- true
- end
-
- # Returns false for names matching <tt>/_before_type_cast$/</tt>,
- # otherwise delegates to super.
- def respond_to?(message, include_private=false)
- message.to_s =~ /_before_type_cast$/ ? false : super
- end
- end
-
- # Starting with Rails 4.1, ActiveRecord associations are inversible
- # by default. This class represents an association from the mocked
- # model's perspective.
- #
- # @private
- class Association
- attr_accessor :target, :inversed
-
- def initialize(association_name)
- @association_name = association_name
- end
- end
-
- module ActiveRecordInstanceMethods
- # Stubs `persisted?` to return `false` and `id` to return `nil`.
- def destroy
- RSpec::Mocks.allow_message(self, :persisted?).and_return(false)
- RSpec::Mocks.allow_message(self, :id).and_return(nil)
- end
-
- # Transforms the key to a method and calls it.
- def [](key)
- send(key)
- end
-
- # Returns the opposite of `persisted?`
- def new_record?
- !persisted?
- end
-
- # Returns an object representing an association from the mocked
- # model's perspective. For use by Rails internally only.
- def association(association_name)
- @associations ||= Hash.new { |h, k| h[k] = Association.new(k) }
- @associations[association_name]
- end
- end
-
- # Creates a test double representing `string_or_model_class` with common
- # ActiveModel methods stubbed out. Additional methods may be easily
- # stubbed (via add_stubs) if `stubs` is passed. This is most useful for
- # impersonating models that don't exist yet.
- #
- # NOTE that only ActiveModel's methods, plus <tt>new_record?</tt>, are
- # stubbed out implicitly. <tt>new_record?</tt> returns the inverse of
- # <tt>persisted?</tt>, and is present only for compatibility with
- # extension frameworks that have yet to update themselves to the
- # ActiveModel API (which declares <tt>persisted?</tt>, not
- # <tt>new_record?</tt>).
- #
- # `string_or_model_class` can be any of:
- #
- # * 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 = {})
- if String === string_or_model_class
- if Object.const_defined?(string_or_model_class)
- model_class = Object.const_get(string_or_model_class)
- else
- model_class = Object.const_set(string_or_model_class, Class.new do
- extend ActiveModel::Naming
- def self.primary_key; :id; end
- end)
- end
- else
- model_class = string_or_model_class
- end
-
- unless model_class.kind_of? ActiveModel::Naming
- raise ArgumentError.new <<-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
- end
-
- stubs = stubs.reverse_merge(:id => next_id)
- stubs = stubs.reverse_merge(:persisted? => !!stubs[:id],
- :destroyed? => false,
- :marked_for_destruction? => false,
- :valid? => true,
- :blank? => false)
-
- double("#{model_class.name}_#{stubs[:id]}", stubs).tap do |m|
- m.singleton_class.class_eval do
- include ActiveModelInstanceMethods
- include ActiveRecordInstanceMethods if defined?(ActiveRecord)
- include ActiveModel::Conversion
- include ActiveModel::Validations
- end
- if defined?(ActiveRecord)
- [:save, :update_attributes, :update].each do |key|
- if stubs[key] == false
- RSpec::Mocks.allow_message(m.errors, :empty?).and_return(false)
- end
- end
- end
-
- msingleton = m.singleton_class
- msingleton.__send__(:define_method, :is_a?) do |other|
- model_class.ancestors.include?(other)
- end unless stubs.has_key?(:is_a?)
-
- msingleton.__send__(:define_method, :kind_of?) do |other|
- model_class.ancestors.include?(other)
- end unless stubs.has_key?(:kind_of?)
-
- msingleton.__send__(:define_method, :instance_of?) do |other|
- other == model_class
- end unless stubs.has_key?(:instance_of?)
-
- msingleton.__send__(:define_method, :__model_class_has_column?) do |method_name|
- model_class.respond_to?(:column_names) && model_class.column_names.include?(method_name.to_s)
- end
-
- msingleton.__send__(:define_method, :respond_to?) do |method_name, *args|
- 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)
- end
-
- msingleton.__send__(:define_method, :class) do
- model_class
- end unless stubs.has_key?(:class)
-
- mock_param = to_param
- msingleton.__send__(:define_method, :to_s) do
- "#{model_class.name}_#{mock_param}"
- end unless stubs.has_key?(:to_s)
- yield m if block_given?
- end
- end
-
- module ActiveModelStubExtensions
- # Stubs `persisted` to return false and `id` to return nil
- def as_new_record
- RSpec::Mocks.allow_message(self, :persisted?).and_return(false)
- RSpec::Mocks.allow_message(self, :id).and_return(nil)
- self
- end
-
- # Returns `true` by default. Override with a stub.
- def persisted?
- true
- end
- end
-
- module ActiveRecordStubExtensions
- # Stubs `id` (or other primary key method) to return nil
- def as_new_record
- self.__send__("#{self.class.primary_key}=", nil)
- super
- end
-
- # Returns the opposite of `persisted?`.
- def new_record?
- !persisted?
- end
-
- # Raises an IllegalDataAccessException (stubbed models are not allowed to access the database)
- # @raises IllegalDataAccessException
- def connection
- raise RSpec::Rails::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
- end
- end
-
- # Creates an instance of `Model` with `to_param` stubbed using a
- # generated value that is unique to each object. If `Model` is an
- # `ActiveRecord` model, it is prohibited from accessing the database*.
- #
- # For each key in `stubs`, if the model has a matching attribute
- # (determined by `respond_to?`) it is simply assigned the submitted values.
- # If the model does not have a matching attribute, the key/value pair is
- # assigned as a stub return value using RSpec's mocking/stubbing
- # framework.
- #
- # <tt>persisted?</tt> is overridden to return the result of !id.nil?
- # This means that by default persisted? will return true. If you want
- # the object to behave as a new record, sending it `as_new_record` will
- # set the id to nil. You can also explicitly set :id => nil, in which
- # case persisted? will return false, but using `as_new_record` makes the
- # example a bit more descriptive.
- #
- # While you can use stub_model in any example (model, view, controller,
- # helper), it is especially useful in view examples, which are
- # inherently more state-based than interaction-based.
- #
- # @example
- #
- # stub_model(Person)
- # stub_model(Person).as_new_record
- # stub_model(Person, :to_param => 37)
- # stub_model(Person) {|person| person.first_name = "David"}
- def stub_model(model_class, stubs={})
- model_class.new.tap do |m|
- m.extend ActiveModelStubExtensions
- if defined?(ActiveRecord) && model_class < ActiveRecord::Base
- m.extend ActiveRecordStubExtensions
- primary_key = model_class.primary_key.to_sym
- stubs = stubs.reverse_merge(primary_key => next_id)
- stubs = stubs.reverse_merge(:persisted? => !!stubs[primary_key])
- else
- stubs = stubs.reverse_merge(:id => next_id)
- stubs = stubs.reverse_merge(:persisted? => !!stubs[:id])
- end
- stubs = stubs.reverse_merge(:blank? => false)
-
- stubs.each do |message, return_value|
- if m.respond_to?("#{message}=")
- m.__send__("#{message}=", return_value)
- else
- RSpec::Mocks.allow_message(m, message).and_return(return_value)
- end
- end
-
- yield m if block_given?
- end
- end
-
- private
-
- @@model_id = 1000
-
- def next_id
- @@model_id += 1
- end
-
- end
- end
-end
-
-RSpec.configuration.include RSpec::Rails::Mocks
View
402 spec/rspec/rails/mocks/mock_model_spec.rb
@@ -1,402 +0,0 @@
-require 'spec_helper'
-
-describe "mock_model(RealModel)" do
- context "given a String" do
- context "that does not represent an existing constant" do
- it "class says it's name" do
- model = mock_model("Foo")
- expect(model.class.name).to eq("Foo")
- end
- end
-
- context "that represents an existing constant" do
- context "that extends ActiveModel::Naming" do
- it "treats the constant as the class" do
- model = mock_model("MockableModel")
- expect(model.class.name).to eq("MockableModel")
- end
- end
-
- context "that does not extend ActiveModel::Naming" do
- it "raises with a helpful message" do
- expect do
- mock_model("String")
- end.to raise_error(ArgumentError)
- end
- end
- end
- end
-
- context "given a class that does not extend ActiveModel::Naming" do
- it "raises with a helpful message" do
- expect do
- mock_model(String)
- end.to raise_error(ArgumentError)
- end
- end
-
- describe "with #id stubbed" do
- before(:each) do
- @model = mock_model(MockableModel, :id => 1)
- end
-
- it "is named using the stubbed id value" do
- expect(@model.instance_variable_get(:@name)).to eq("MockableModel_1")
- end
- end
-
- describe "destroy" do
- it "sets persisted to false" do
- model = mock_model(MockableModel)
- model.destroy
- expect(model).not_to be_persisted
- end
- end
-
- describe "association" do
- it "constructs a mock association object" do
- model = mock_model(MockableModel)
- expect(model.association(:association_name)).to be
- end
-
- it "returns a different association object for each association name" do
- model = mock_model(MockableModel)
- posts = model.association(:posts)
- authors = model.association(:authors)
-
- expect(posts).not_to equal(authors)
- end
-
- it "returns the same association model each time for the same association name" do
- model = mock_model(MockableModel)
- posts1 = model.association(:posts)
- posts2 = model.association(:posts)
-
- expect(posts1).to equal(posts2)
- end
- end
-
- describe "errors" do
- context "default" do
- it "is empty" do
- model = mock_model(MockableModel)
- expect(model.errors).to be_empty
- end
- end
-
- context "with :save => false" do
- it "is not empty" do
- model = mock_model(MockableModel, :save => false)
- expect(model.errors).not_to be_empty
- end
- end
-
- context "with :update_attributes => false" do
- it "is not empty" do
- model = mock_model(MockableModel, :save => false)
- expect(model.errors).not_to be_empty
- end
- end
- end
-
- describe "with params" do
- it "does not mutate its parameters" do
- params = {:a => 'b'}
- mock_model(MockableModel, params)
- expect(params).to eq({:a => 'b'})
- end
- end
-
- describe "as association" do
- before(:each) do
- @real = AssociatedModel.create!
- @mock_model = mock_model(MockableModel)
- @real.mockable_model = @mock_model
- end
-
- it "passes: associated_model == mock" do
- expect(@mock_model).to eq(@real.mockable_model)
- end
-
- it "passes: mock == associated_model" do
- expect(@real.mockable_model).to eq(@mock_model)
- end
- end
-
- describe "as association that doesn't exist yet" do
- before(:each) do
- @real = AssociatedModel.create!
- @mock_model = mock_model("Other")
- @real.nonexistent_model = @mock_model
- end
-
- it "passes: associated_model == mock" do
- expect(@mock_model).to eq(@real.nonexistent_model)
- end
-
- it "passes: mock == associated_model" do
- expect(@real.nonexistent_model).to eq(@mock_model)
- end
- end
-
- describe "#is_a?" do
- before(:each) do
- @model = mock_model(SubMockableModel)
- end
-
- it "says it is_a?(RealModel)" do
- expect(@model.is_a?(SubMockableModel)).to be(true)
- end
-
- it "says it is_a?(OtherModel) if RealModel is an ancestors" do
- expect(@model.is_a?(MockableModel)).to be(true)
- end
-
- it "can be stubbed" do
- expect(mock_model(MockableModel, :is_a? => true).is_a?(:Foo)).to be_truthy
- end
- end
-
- describe "#kind_of?" do
- before(:each) do
- @model = mock_model(SubMockableModel)
- end
-
- it "says it is kind_of? if RealModel is" do
- expect(@model.kind_of?(SubMockableModel)).to be(true)
- end
-
- it "says it is kind_of? if RealModel's ancestor is" do
- expect(@model.kind_of?(MockableModel)).to be(true)
- end
-
- it "can be stubbed" do
- expect(mock_model(MockableModel, :kind_of? => true).kind_of?(:Foo)).to be_truthy
- end
- end
-
- describe "#instance_of?" do
- before(:each) do
- @model = mock_model(SubMockableModel)
- end
-
- it "says it is instance_of? if RealModel is" do
- expect(@model.instance_of?(SubMockableModel)).to be(true)
- end
-
- it "does not say it instance_of? if RealModel isn't, even if it's ancestor is" do
- expect(@model.instance_of?(MockableModel)).to be(false)
- end
-
- it "can be stubbed" do
- expect(mock_model(MockableModel, :instance_of? => true).instance_of?(:Foo)).to be_truthy
- end
- end
-
- describe "#respond_to?" do
- context "with an ActiveRecord model" do
- before(:each) do
- allow(MockableModel).to receive(:column_names).and_return(["column_a", "column_b"])
- @model = mock_model(MockableModel)
- end
-
- it "accepts two arguments" do
- expect do
- @model.respond_to?("title_before_type_cast", false)
- end.to_not raise_exception
- end
-
- context "without as_null_object" do
- it "says it will respond_to?(key) if RealModel has the attribute 'key'" do
- expect(@model.respond_to?("column_a")).to be(true)
- end
- it "stubs column accessor (with string)" do
- @model.respond_to?("column_a")
- expect(@model.column_a).to be_nil
- end
- it "stubs column accessor (with symbol)" do
- @model.respond_to?(:column_a)
- expect(@model.column_a).to be_nil
- end
- it "does not stub column accessor if already stubbed in declaration (with string)" do
- model = mock_model(MockableModel, "column_a" => "a")
- model.respond_to?("column_a")
- expect(model.column_a).to eq("a")
- end
- it "does not stub column accessor if already stubbed in declaration (with symbol)" do
- model = mock_model(MockableModel, :column_a => "a")
- model.respond_to?("column_a")
- expect(model.column_a).to eq("a")
- end
- it "does not stub column accessor if already stubbed after declaration (with string)" do
- allow(@model).to receive_messages("column_a" => "a")
- @model.respond_to?("column_a")
- expect(@model.column_a).to eq("a")
- end
- it "does not stub column accessor if already stubbed after declaration (with symbol)" do
- allow(@model).to receive_messages(:column_a => "a")
- @model.respond_to?("column_a")
- expect(@model.column_a).to eq("a")
- end
- it "says it will not respond_to?(key) if RealModel does not have the attribute 'key'" do
- expect(@model.respond_to?("column_c")).to be(false)
- end
- it "says it will not respond_to?(xxx_before_type_cast)" do
- expect(@model.respond_to?("title_before_type_cast")).to be(false)
- end
- end
-
- context "with as_null_object" do
- it "says it will respond_to?(key) if RealModel has the attribute 'key'" do
- expect(@model.as_null_object.respond_to?("column_a")).to be(true)
- end
- it "says it will respond_to?(key) even if RealModel does not have the attribute 'key'" do
- expect(@model.as_null_object.respond_to?("column_c")).to be(true)
- end
- it "says it will not respond_to?(xxx_before_type_cast)" do
- expect(@model.as_null_object.respond_to?("title_before_type_cast")).to be(false)
- end
- it "returns self for any unprepared message" do
- @model.as_null_object.tap do |x|
- expect(x.non_existant_message).to be(@model)
- end
- end
- end
- end
-
- context "with a non-ActiveRecord model" do
- it "responds as normal" do
- model = NonActiveRecordModel.new
- expect(model).to respond_to(:to_param)
- end
-
- context "with as_null_object" do
- it "says it will not respond_to?(xxx_before_type_cast)" do
- model = NonActiveRecordModel.new.as_null_object
- expect(model.respond_to?("title_before_type_cast")).to be(false)
- end
- end
- end
-
- it "can be stubbed" do
- expect(mock_model(MockableModel, :respond_to? => true).respond_to?(:foo)).to be_truthy
- end
- end
-
- describe "#class" do
- it "returns the mocked model" do
- expect(mock_model(MockableModel).class).to eq(MockableModel)
- end
-
- it "can be stubbed" do
- expect(mock_model(MockableModel, :class => String).class).to be(String)
- end
- end
-
- describe "#to_s" do
- it "returns (model.name)_(model#to_param)" do
- expect(mock_model(MockableModel).to_s).to eq("MockableModel_#{to_param}")
- end
-
- it "can be stubbed" do
- expect(mock_model(MockableModel, :to_s => "this string").to_s).to eq("this string")
- end
- end
-
- describe "#destroyed?" do
- context "default" do
- it "returns false" do
- @model = mock_model(SubMockableModel)
- expect(@model.destroyed?).to be(false)
- end
- end
- end
-
- describe "#marked_for_destruction?" do
- context "default" do
- it "returns false" do
- @model = mock_model(SubMockableModel)
- expect(@model.marked_for_destruction?).to be(false)
- end
- end
- end
-
- describe "#persisted?" do
- context "with default identifier" do
- it "returns true" do
- expect(mock_model(MockableModel)).to be_persisted
- end
- end
-
- context "with explicit identifier via :id" do
- it "returns true" do
- expect(mock_model(MockableModel, :id => 37)).to be_persisted
- end
- end
-
- context "with id => nil" do
- it "returns false" do
- expect(mock_model(MockableModel, :id => nil)).not_to be_persisted
- end
- end
- end
-
- describe "#valid?" do
- context "default" do
- it "returns true" do
- expect(mock_model(MockableModel)).to be_valid
- end
- end
-
- context "stubbed with false" do
- it "returns false" do
- expect(mock_model(MockableModel, :valid? => false)).not_to be_valid
- end
- end
- end
-
- describe "#as_new_record" do
- it "says it is a new record" do
- m = mock_model(MockableModel)
- expect(m.as_new_record).to be_new_record
- end
-
- it "says it is not persisted" do
- m = mock_model(MockableModel)
- expect(m.as_new_record).not_to be_persisted
- end
-
- it "has a nil id" do
- expect(mock_model(MockableModel).as_new_record.id).to be(nil)
- end
-
- it "returns nil for #to_param" do
- expect(mock_model(MockableModel).as_new_record.to_param).to be(nil)
- end
- end
-
- describe "#blank?" do
- it "is false" do
- expect(mock_model(MockableModel)).not_to be_blank
- end
- end
-
- describe "ActiveModel Lint tests" do
- require 'active_model/lint'
- include RSpec::Rails::MinitestAssertionAdapter
- 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
- with_isolated_stderr do
- send m
- end
- end
- end
-
- def model
- mock_model(MockableModel, :id => nil)
- end
- end
-end
View
154 spec/rspec/rails/mocks/stub_model_spec.rb
@@ -1,154 +0,0 @@
-require 'spec_helper'
-
-describe "stub_model" do
-
- shared_examples_for "stub model" do
- describe "with a block" do
- it "yields the model" do
- model = stub_model(model_class) do |block_arg|
- @block_arg = block_arg
- end
- expect(model).to be(@block_arg)
- end
- end
-
- describe "#persisted?" do
- context "default" do
- it "returns true" do
- model = stub_model(model_class)
- expect(model).to be_persisted
- end
- end
-
- context "with as_new_record" do
- it "returns false" do
- model = stub_model(model_class).as_new_record
- expect(model).not_to be_persisted
- end
- end
- end
-
- it "increments the value returned by to_param" do
- first = stub_model(model_class)
- second = stub_model(model_class)
- expect(second.to_param.to_i).to eq(first.to_param.to_i + 1)
- end
-
- describe "#blank?" do
- it "is false" do
- expect(stub_model(model_class)).not_to be_blank
- end
- end
- end
-
- context "with ActiveModel (not ActiveRecord)" do
- it_behaves_like "stub model" do
- def model_class
- NonActiveRecordModel
- end
- end
- end
-
- context "with an ActiveRecord model" do
- let(:model_class) { MockableModel }
-
- it_behaves_like "stub model"
-
- describe "#new_record?" do
- context "default" do
- it "returns false" do
- model = stub_model(model_class)
- expect(model.new_record?).to be_falsey
- end
- end
-
- context "with as_new_record" do
- it "returns true" do
- model = stub_model(model_class).as_new_record
- expect(model.new_record?).to be_truthy
- end
- end
- end
-
- describe "defaults" do
- it "has an id" do
- expect(stub_model(MockableModel).id).to be > 0
- end
-
- it "says it is not a new record" do
- expect(stub_model(MockableModel)).not_to be_new_record
- end
- end
-
- describe "#as_new_record" do
- it "has a nil id" do
- expect(stub_model(MockableModel).as_new_record.id).to be(nil)
- end
- end
-
- it "raises when hitting the db" do
- expect do
- stub_model(MockableModel).connection
- end.to raise_error(RSpec::Rails::IllegalDataAccessException, /stubbed models are not allowed to access the database/)
- end
-
- it "increments the id" do
- first = stub_model(model_class)
- second = stub_model(model_class)
- expect(second.id).to eq(first.id + 1)
- end
-
- it "accepts a stub id" do
- expect(stub_model(MockableModel, :id => 37).id).to eq(37)
- end
-
- it "says it is a new record when id is set to nil" do
- expect(stub_model(MockableModel, :id => nil)).to be_new_record
- end
-
- it "accepts a stub for save" do
- expect(stub_model(MockableModel, :save => false).save).to be(false)
- end
-
- describe "alternate primary key" do
- it "has the correct primary_key name" do
- expect(stub_model(AlternatePrimaryKeyModel).class.primary_key.to_s).to eq('my_id')
- end
-
- it "has a primary_key" do
- expect(stub_model(AlternatePrimaryKeyModel).my_id).to be > 0
- end
-
- it "says it is not a new record" do
- stub_model(AlternatePrimaryKeyModel) do |m|
- expect(m).not_to be_new_record
- end
- end
-
- it "says it is a new record if primary_key is nil" do
- expect(stub_model(AlternatePrimaryKeyModel, :my_id => nil)).to be_new_record
- end
-
- it "accepts a stub for the primary_key" do
- expect(stub_model(AlternatePrimaryKeyModel, :my_id => 5).my_id).to eq(5)
- end
- end
-
- describe "as association" do
- before(:each) do
- @real = AssociatedModel.create!
- @stub_model = stub_model(MockableModel)
- @real.mockable_model = @stub_model
- end
-
- it "passes associated_model == mock" do
- expect(@stub_model).to eq(@real.mockable_model)
- end
-
- it "passes mock == associated_model" do
- expect(@real.mockable_model).to eq(@stub_model)
- end
- end
-
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.