Permalink
Browse files

reorg a spec

  • Loading branch information...
dchelimsky committed May 20, 2010
1 parent b405234 commit d7f39ffebd322b5ea44298dc8af26c7ccce1d526
Showing with 96 additions and 74 deletions.
  1. +86 −64 spec/rspec/rails/mocks/mock_model_spec.rb
  2. +10 −10 specs.watchr
@@ -1,116 +1,142 @@
require 'spec_helper'
require File.dirname(__FILE__) + '/ar_classes'
describe "mock_model" do
describe "responding to interrogation" do
describe "mock_model(RealModel)" do
describe "with #id stubbed" do
before(:each) do
@model = mock_model(SubMockableModel)
end
it "should say it is_a? if it is" do
@model.is_a?(SubMockableModel).should be(true)
end
it "should say it is_a? if it's ancestor is" do
@model.is_a?(MockableModel).should be(true)
end
it "should say it is kind_of? if it is" do
@model.kind_of?(SubMockableModel).should be(true)
end
it "should say it is kind_of? if it's ancestor is" do
@model.kind_of?(MockableModel).should be(true)
end
it "should say it is instance_of? if it is" do
@model.instance_of?(SubMockableModel).should be(true)
end
it "should not say it instance_of? if it isn't, even if it's ancestor is" do
@model.instance_of?(MockableModel).should be(false)
@model = mock_model(MockableModel, :id => 1)
end
it "should say it is not destroyed" do
@model.destroyed?(SubMockableModel).should be(false)
it "is named using the stubbed id value" do
@model.instance_variable_get(:@name).should == "MockableModel_1"
end
it "should say it is not marked_for_destruction" do
@model.marked_for_destruction?.should be(false)
it "returns string of id value for to_param" do
@model.to_param.should == "1"
end
end
describe "with params" do
it "should not mutate its parameters" do
it "does not mutate its parameters" do
params = {:a => 'b'}
model = mock_model(MockableModel, params)
params.should == {:a => 'b'}
end
end
describe "with #id stubbed", :type => :view do
describe "as association" do
before(:each) do
@model = mock_model(MockableModel, :id => 1)
@real = AssociatedModel.create!
@mock_model = mock_model(MockableModel)
@real.mockable_model = @mock_model
end
it "should be named using the stubbed id value" do
@model.instance_variable_get(:@name).should == "MockableModel_1"
it "passes: associated_model == mock" do
@mock_model.should == @real.mockable_model
end
it "should return string of id value for to_param" do
@model.to_param.should == "1"
it "passes: mock == associated_model" do
@real.mockable_model.should == @mock_model
end
end
describe "#is_a?" do
before(:each) do
@model = mock_model(SubMockableModel)
end
it "says it is_a?(RealModel)" do
@model.is_a?(SubMockableModel).should be(true)
end
it "says it is_a?(OtherModel) if RealModel is an ancestors" do
@model.is_a?(MockableModel).should be(true)
end
end
describe "#kind_of?" do
before(:each) do
@model = mock_model(SubMockableModel)
end
it "says it is kind_of? if RealModel is" do
@model.kind_of?(SubMockableModel).should be(true)
end
it "says it is kind_of? if RealModel's ancestor is" do
@model.kind_of?(MockableModel).should be(true)
end
end
context "with id nil" do
it "is not persisted" do
mock_model(MockableModel, :id => nil).should_not be_persisted
describe "#instance_of?" do
before(:each) do
@model = mock_model(SubMockableModel)
end
it "says it is instance_of? if RealModel is" do
@model.instance_of?(SubMockableModel).should be(true)
end
it "does not say it instance_of? if RealModel isn't, even if it's ancestor is" do
@model.instance_of?(MockableModel).should be(false)
end
end
describe "valid?" do
describe "#destroyed?" do
context "default" do
it "returns true" do
mock_model(MockableModel).should be_valid
it "returns false" do
@model = mock_model(SubMockableModel)
@model.destroyed?.should be(false)
end
end
context "stubbed with false" do
end
describe "#marked_for_destruction?" do
context "default" do
it "returns false" do
mock_model(MockableModel, :valid? => false).should_not be_valid
@model = mock_model(SubMockableModel)
@model.marked_for_destruction?.should be(false)
end
end
end
describe "as association", :type => :view do
before(:each) do
@real = AssociatedModel.create!
@mock_model = mock_model(MockableModel)
@real.mockable_model = @mock_model
describe "#persisted?" do
context "with default id" do
it "returns true" do
mock_model(MockableModel).should be_persisted
end
end
it "should pass associated_model == mock" do
@mock_model.should == @real.mockable_model
context "with explicit id" do
it "returns true" do
mock_model(MockableModel, :id => 37).should be_persisted
end
end
it "should pass mock == associated_model" do
@real.mockable_model.should == @mock_model
context "with id nil" do
it "returns false" do
mock_model(MockableModel, :id => nil).should_not be_persisted
end
end
end
describe "#as_null_object" do
before(:each) do
@model = mock_model(MockableModel, :mocked_method => "mocked").as_null_object
end
it "should be able to mock methods" do
@model.mocked_method.should == "mocked"
describe "#valid?" do
context "default" do
it "returns true" do
mock_model(MockableModel).should be_valid
end
end
it "should return itself to unmocked methods" do
@model.unmocked_method.should equal(@model)
context "stubbed with false" do
it "returns false" do
mock_model(MockableModel, :valid? => false).should_not be_valid
end
end
end
describe "#as_new_record" do
it "should say it is a new record" do
it "says it is a new record" do
m = mock_model(MockableModel)
m.as_new_record.should be_new_record
end
it "should have a nil id" do
it "has a nil id" do
mock_model(MockableModel).as_new_record.id.should be(nil)
end
it "should return nil for #to_param" do
it "returns nil for #to_param" do
mock_model(MockableModel).as_new_record.to_param.should be(nil)
end
end
@@ -133,7 +159,3 @@ def model
end
end
View
@@ -5,12 +5,12 @@
# --------------------------------------------------
# Convenience Methods
# --------------------------------------------------
def all_test_files
def all_spec_files
Dir['spec/**/*_spec.rb']
end
def run_test_matching(thing_to_match)
matches = all_test_files.grep(/#{thing_to_match}/i)
def run_spec_matching(thing_to_match)
matches = all_spec_files.grep(/#{thing_to_match}/i)
if matches.empty?
puts "Sorry, thanks for playing, but there were no matches for #{thing_to_match}"
else
@@ -24,17 +24,17 @@ def run(files_to_run)
no_int_for_you
end
def run_all_tests
run(all_test_files.join(' '))
def run_all_specs
run(all_spec_files.join(' '))
end
# --------------------------------------------------
# Watchr Rules
# --------------------------------------------------
watch('^spec/(.*)_spec\.rb') { |m| run_test_matching(m[1]) }
watch('^lib/(.*)\.rb') { |m| run_test_matching(m[1]) }
watch('^spec/spec_helper\.rb') { run_all_tests }
watch('^spec/support/.*\.rb') { run_all_tests }
watch('^spec/(.*)_spec\.rb') { |m| run_spec_matching(m[1]) }
watch('^lib/(.*)\.rb') { |m| run_spec_matching(m[1]) }
watch('^spec/spec_helper\.rb') { run_all_specs }
watch('^spec/support/.*\.rb') { run_all_specs }
# --------------------------------------------------
# Signal Handling
@@ -52,7 +52,7 @@ Signal.trap 'INT' do
puts " Did you just send me an INT? Ugh. I'll quit for real if you do it again."
@sent_an_int = true
Kernel.sleep 1.5
run_all_tests
run_all_specs
end
end

0 comments on commit d7f39ff

Please sign in to comment.