Skip to content

Commit

Permalink
mock_model passes ActiveModel::Lint::Tests
Browse files Browse the repository at this point in the history
Closes rspec#23
Closes rspec#24
  • Loading branch information
dchelimsky committed Apr 26, 2010
1 parent 24ca705 commit b404c2d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 25 deletions.
35 changes: 24 additions & 11 deletions lib/rspec/rails/mocks.rb
Expand Up @@ -4,29 +4,42 @@ module Rails
class IllegalDataAccessException < StandardError; end

module Mocks

module InstanceMethods
def valid?
true
end

def as_new_record
self.stub(:id) { nil }
self
end

def new_record?
!persisted?
end

def persisted?
!!id
end
end

# Creates a mock object instance for a +model_class+ with common
# methods stubbed out. Additional methods may be easily stubbed (via
# add_stubs) if +stubs+ is passed.
def mock_model(model_class, options_and_stubs = {})
id = options_and_stubs[:id] || next_id
id = options_and_stubs.has_key?(:id) ? options_and_stubs[:id] : next_id
options_and_stubs = options_and_stubs.reverse_merge({
:id => id,
:to_param => id.to_s,
:new_record? => false,
:destroyed? => false,
:marked_for_destruction? => false,
:errors => stub("errors", :count => 0, :any? => false)
:marked_for_destruction? => false
})
derived_name = "#{model_class.name}_#{id}"
m = mock(derived_name, options_and_stubs)
m.extend InstanceMethods
m.extend ActiveModel::Conversion
m.stub(:errors) { ActiveModel::Errors.new(m) }
m.__send__(:__mock_proxy).instance_eval(<<-CODE, __FILE__, __LINE__)
def @object.as_new_record
self.stub(:id) { nil }
self.stub(:to_param) { nil }
self.stub(:new_record?) { true }
self
end
def @object.is_a?(other)
#{model_class}.ancestors.include?(other)
end
Expand Down
53 changes: 39 additions & 14 deletions spec/rspec/rails/mocks/mock_model_spec.rb
Expand Up @@ -52,6 +52,25 @@
end
end

context "with id nil" do
it "is not persisted" do
mock_model(MockableModel, :id => nil).should_not be_persisted
end
end

describe "valid?" do
context "default" do
it "returns true" do
mock_model(MockableModel).should be_valid
end
end
context "stubbed with false" do
it "returns false" do
mock_model(MockableModel, :valid? => false).should_not be_valid
end
end
end

describe "as association", :type => :view do
before(:each) do
@real = AssociatedModel.create!
Expand All @@ -68,20 +87,7 @@
end
end

describe "with :null_object => true", :type => :view do
before(:each) do
@model = mock_model(MockableModel, :null_object => true, :mocked_method => "mocked")
end

it "should be able to mock methods" do
@model.mocked_method.should == "mocked"
end
it "should return itself to unmocked methods" do
@model.unmocked_method.should equal(@model)
end
end

describe "#as_null_object", :type => :view do
describe "#as_null_object" do
before(:each) do
@model = mock_model(MockableModel, :mocked_method => "mocked").as_null_object
end
Expand All @@ -108,7 +114,26 @@
mock_model(MockableModel).as_new_record.to_param.should be(nil)
end
end

describe "ActiveModel Lint tests" do
require 'test/unit/assertions'
require 'active_model/lint'
include Test::Unit::Assertions
include ActiveModel::Lint::Tests

ActiveModel::Lint::Tests.public_instance_methods.grep(/^test/).each do |m|
example m.gsub('_',' ') do
send m
end
end

def model
mock_model(MockableModel, :id => nil)
end

end
end




0 comments on commit b404c2d

Please sign in to comment.