Skip to content
Browse files

Fix to make mocks work

  • Loading branch information...
1 parent 5684f19 commit 2f0349da0800c6496d5c35e2160456e38ad254ca @glv committed Sep 13, 2010
Showing with 34 additions and 84 deletions.
  1. +11 −3 spec/spec_helper.rb
  2. +23 −81 spec/test_case_spec.rb
View
14 spec/spec_helper.rb
@@ -5,6 +5,12 @@
require 'rspec-unit'
+def mock_example_group_instance(example_group)
+ eg_inst = example_group.new
+ example_group.stub(:new).and_return(eg_inst)
+ eg_inst
+end
+
class NullObject
def method_missing(method, *args, &block)
# ignore
@@ -13,7 +19,11 @@ def method_missing(method, *args, &block)
class RSpec::Core::ExampleGroup
def self.run_all(reporter=nil)
+ @orig_space = RSpec::Mocks.space
+ RSpec::Mocks.space = RSpec::Mocks::Space.new
run(reporter || NullObject.new)
+ ensure
+ RSpec::Mocks.space = @orig_space
end
end
@@ -41,12 +51,10 @@ def in_editor?
end
RSpec.configure do |c|
- # c.mock_with :rspec
+ c.color_enabled = !in_editor?
c.filter_run :focused => true
c.run_all_when_everything_filtered = true
- c.color_enabled = !in_editor?
c.alias_example_to :fit, :focused => true
- c.profile_examples = false
c.formatter = :documentation
c.around do |example|
sandboxed { example.run }
View
104 spec/test_case_spec.rb
@@ -95,86 +95,49 @@ def test_bar; end
describe "running test methods" do
it "runs the test methods as examples" do
- @foo.metadata[:example_group][:invocation_count] = 0
- @foo.class_eval do
- def test_bar; self.class.metadata[:example_group][:invocation_count] += 1; end
- end
-
- @foo.run_all
- @foo.metadata[:example_group][:invocation_count].should == 1
- end
-
- it "runs the test methods as examples (with mocks)" do
- pending "RSpec is not properly checking mock expectations"
@foo.class_eval do
def test_bar; end
end
- @foo.should_receive(:test_bar).once
+
+ eg_inst = mock_example_group_instance(@foo)
+ eg_inst.should_receive(:test_bar).once
+
@foo.run_all
end
it "brackets test methods with setup/teardown" do
- @foo.metadata[:example_group][:invocations] = []
- @foo.class_eval do
- def setup; self.class.metadata[:example_group][:invocations] << 'setup'; end
- def teardown; self.class.metadata[:example_group][:invocations] << 'teardown'; end
- def test_bar; self.class.metadata[:example_group][:invocations] << 'test_bar'; end
- def test_baz; self.class.metadata[:example_group][:invocations] << 'test_baz'; end
- end
-
- @foo.run_all
- @foo.metadata[:example_group][:invocations].should == %w[setup test_bar teardown setup test_baz teardown]
- end
-
- it "brackets test methods with setup/teardown (with mocks)" do
- pending "RSpec is not properly checking mock expectations"
@foo.class_eval do
def test_bar; end
def test_baz; end
end
- @foo.should_receive(:setup) .once.ordered
- @foo.should_receive(:test_bar).once.ordered
- @foo.should_receive(:teardown).once.ordered
- @foo.should_receive(:setup) .once.ordered
- @foo.should_receive(:test_baz).once.ordered
- @foo.should_receive(:teardown).once.ordered
+ eg_inst = mock_example_group_instance(@foo)
+ eg_inst.should_receive(:setup) .once.ordered
+ eg_inst.should_receive(:test_bar).once.ordered
+ eg_inst.should_receive(:teardown).once.ordered
+ eg_inst.should_receive(:setup) .once.ordered
+ eg_inst.should_receive(:test_baz).once.ordered
+ eg_inst.should_receive(:teardown).once.ordered
@foo.run_all
end
it "only calls setup/teardown once per test in subclasses" do
@foo.class_eval do
- def test_foo; self.class.metadata[:example_group][:invocations] << 'test_foo'; end
- end
- bar = Class.new(@foo)
- bar.metadata[:example_group][:invocations] = []
- bar.class_eval do
- def setup; self.class.metadata[:example_group][:invocations] << 'setup'; end
- def teardown; self.class.metadata[:example_group][:invocations] << 'teardown'; end
- def test_bar; self.class.metadata[:example_group][:invocations] << 'test_bar'; end
- end
-
- bar.run_all
- bar.metadata[:example_group][:invocations].should == %w[setup test_bar teardown setup test_foo teardown]
- end
-
- it "only calls setup/teardown once per test in subclasses (with mocks)" do
- pending "RSpec is not properly checking mock expectations"
- @foo.class_eval do
def test_baz; end
end
bar = Class.new(@foo)
bar.class_eval do
def test_quux; end
end
- bar.should_receive(:setup) .once
- bar.should_receive(:test_baz) .once
- bar.should_receive(:teardown) .once
- bar.should_receive(:setup) .once
- bar.should_receive(:test_quux).once
- bar.should_receive(:teardown) .once
+ eg_inst = mock_example_group_instance(bar)
+ eg_inst.should_receive(:setup) .once.ordered
+ eg_inst.should_receive(:test_baz) .once.ordered
+ eg_inst.should_receive(:teardown) .once.ordered
+ eg_inst.should_receive(:setup) .once.ordered
+ eg_inst.should_receive(:test_quux).once.ordered
+ eg_inst.should_receive(:teardown) .once.ordered
bar.run_all
end
@@ -396,46 +359,25 @@ def test_quux; end
end
it "allows defining 'before' blocks" do
- invocation_count = 0
- @foo.class_eval do
- before {invocation_count += 1}
- def test_bar; end
- end
-
- @foo.run_all
- invocation_count.should == 1
- end
-
- it "allows defining 'before' blocks (with mocks)" do
- pending "RSpec is not properly checking mock expectations"
@foo.class_eval do
before {bar}
def test_bar; end
end
- @foo.should_receive(:bar).once
+ eg_inst = mock_example_group_instance(@foo)
+ eg_inst.should_receive(:bar).once
+
@foo.run_all
end
it "allows defining 'after' blocks" do
- invocation_count = 0
- @foo.class_eval do
- after {invocation_count += 1}
- def test_bar; end
- end
-
- @foo.run_all
- invocation_count.should == 1
- end
-
- it "allows defining 'after' blocks (with mocks)" do
- pending "RSpec is not properly checking mock expectations"
@foo.class_eval do
after {bar}
def test_bar; end
end
- @foo.should_receive(:bar).once
+ eg_inst = mock_example_group_instance(@foo)
+ eg_inst.should_receive(:bar).once
@foo.run_all
end

0 comments on commit 2f0349d

Please sign in to comment.
Something went wrong with that request. Please try again.