Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move some specs around

  • Loading branch information...
commit 5f02fb6a7f4f9d0e18cdf6c5f1a32d806bc2fc00 1 parent 6551f1a
@dchelimsky dchelimsky authored
View
6 spec/rspec/mocks/and_yield_spec.rb
@@ -1,9 +1,9 @@
require 'spec_helper'
-def obj
- @obj ||= mock
-end
describe RSpec::Mocks::Mock do
+
+ let(:obj) { double }
+
describe "#and_yield" do
context "with eval context as block argument" do
View
50 spec/rspec/mocks/any_number_of_times_spec.rb
@@ -1,36 +1,30 @@
require 'spec_helper'
-module RSpec
- module Mocks
-
- describe "AnyNumberOfTimes" do
- before(:each) do
- @mock = RSpec::Mocks::Mock.new("test mock")
- end
+describe "AnyNumberOfTimes" do
+ before(:each) do
+ @mock = RSpec::Mocks::Mock.new("test mock")
+ end
- it "passes if any number of times method is called many times" do
- @mock.should_receive(:random_call).any_number_of_times
- (1..10).each do
- @mock.random_call
- end
- end
+ it "passes if any number of times method is called many times" do
+ @mock.should_receive(:random_call).any_number_of_times
+ (1..10).each do
+ @mock.random_call
+ end
+ end
- it "passes if any number of times method is called once" do
- @mock.should_receive(:random_call).any_number_of_times
- @mock.random_call
- end
-
- it "passes if any number of times method is not called" do
- @mock.should_receive(:random_call).any_number_of_times
- end
+ it "passes if any number of times method is called once" do
+ @mock.should_receive(:random_call).any_number_of_times
+ @mock.random_call
+ end
- it "returns the mocked value when called after a similar stub" do
- @mock.stub(:message).and_return :stub_value
- @mock.should_receive(:message).any_number_of_times.and_return(:mock_value)
- @mock.message.should == :mock_value
- @mock.message.should == :mock_value
- end
- end
+ it "passes if any number of times method is not called" do
+ @mock.should_receive(:random_call).any_number_of_times
+ end
+ it "returns the mocked value when called after a similar stub" do
+ @mock.stub(:message).and_return :stub_value
+ @mock.should_receive(:message).any_number_of_times.and_return(:mock_value)
+ @mock.message.should == :mock_value
+ @mock.message.should == :mock_value
end
end
View
26 spec/rspec/mocks/bug_report_10263_spec.rb
@@ -1,26 +1,24 @@
-describe "Mock" do
- before do
- @double = double("test double")
- end
-
- specify "when one example has an expectation (non-mock) inside the block passed to the mock" do
- @double.should_receive(:msg) do |b|
- b.should be_true #this call exposes the problem
+describe "Double" do
+ let(:test_double) { double }
+
+ specify "when one example has an expectation inside the block passed to should_receive" do
+ test_double.should_receive(:msg) do |arg|
+ arg.should be_true #this call exposes the problem
end
begin
- @double.msg(false)
+ test_double.msg(false)
rescue Exception
end
end
specify "then the next example should behave as expected instead of saying" do
- @double.should_receive(:foobar)
- @double.foobar
- @double.rspec_verify
+ test_double.should_receive(:foobar)
+ test_double.foobar
+ test_double.rspec_verify
begin
- @double.foobar
+ test_double.foobar
rescue Exception => e
- e.message.should == "Double \"test double\" received unexpected message :foobar with (no args)"
+ e.message.should == "Double received unexpected message :foobar with (no args)"
end
end
end
View
6 spec/rspec/mocks/bug_report_7611_spec.rb
@@ -2,11 +2,9 @@
module Bug7611
describe "A Partial Mock" do
- class Foo
- end
+ class Foo; end
+ class Bar < Foo; end
- class Bar < Foo
- end
it "respects subclasses" do
Foo.stub(:new).and_return(Object.new)
end
View
22 spec/rspec/mocks/bug_report_7805_spec.rb
@@ -1,22 +0,0 @@
-require 'spec_helper'
-
-module Bug7805
- #This is really a duplicate of 8302
-
- describe "Stubs should correctly restore module methods" do
- it "1 - stub the open method" do
- File.stub(:open).and_return("something")
- File.open.should == "something"
- end
- it "2 - use File.open to create example.txt" do
- filename = "#{File.dirname(__FILE__)}/example-#{Time.new.to_i}.txt"
- File.exist?(filename).should be_false
- file = File.open(filename,'w')
- file.close
- File.exist?(filename).should be_true
- File.delete(filename)
- File.exist?(filename).should be_false
- end
- end
-
-end
View
26 spec/rspec/mocks/bug_report_8302_spec.rb
@@ -1,26 +0,0 @@
-require 'spec_helper'
-
-module Bug8302
- describe "Bug report 8302:" do
- class Foo
- def Foo.class_method(arg)
- end
-
- def instance_bar(arg)
- end
- end
-
- it "class method is not restored correctly when proxied" do
- Foo.should_not_receive(:class_method).with(Array.new)
- Foo.rspec_verify
- Foo.class_method(Array.new)
- end
-
- it "instance method is not restored correctly when proxied" do
- foo = Foo.new
- foo.should_not_receive(:instance_bar).with(Array.new)
- foo.rspec_verify
- foo.instance_bar(Array.new)
- end
- end
-end
View
125 spec/rspec/mocks/stub_spec.rb
@@ -31,7 +31,7 @@ def existing_private_instance_method
[:stub!, :stub].each do |method|
describe "using #{method}" do
- it "returns expected value when expected message is received" do
+ it "returns declared value when message is received" do
@instance.send(method, :msg).and_return(:return_value)
@instance.msg.should equal(:return_value)
@instance.rspec_verify
@@ -39,92 +39,95 @@ def existing_private_instance_method
end
end
- it "ignores when expected message is received" do
+ it "instructs an instance to respond_to the message" do
+ @instance.stub(:msg)
+ @instance.should respond_to(:msg)
+ end
+
+ it "instructs a class object to respond_to the message" do
+ @class.stub(:msg)
+ @class.should respond_to(:msg)
+ end
+
+ it "ignores when expected message is received with no args" do
@instance.stub(:msg)
@instance.msg
- lambda do
+ expect do
@instance.rspec_verify
- end.should_not raise_error
+ end.not_to raise_error
end
it "ignores when message is received with args" do
@instance.stub(:msg)
@instance.msg(:an_arg)
- lambda do
+ expect do
@instance.rspec_verify
- end.should_not raise_error
+ end.not_to raise_error
end
it "ignores when expected message is not received" do
@instance.stub(:msg)
- lambda do
+ expect do
@instance.rspec_verify
- end.should_not raise_error
+ end.not_to raise_error
end
it "handles multiple stubbed methods" do
@instance.stub(:msg1 => 1, :msg2 => 2)
- @instance.msg1.should == 1
- @instance.msg2.should == 2
- end
-
- it "clears itself when verified" do
- @instance.stub(:this_should_go).and_return(:blah)
- @instance.this_should_go.should == :blah
- @instance.rspec_verify
- lambda do
- @instance.this_should_go
- end.should raise_error(NameError)
+ @instance.msg1.should eq(1)
+ @instance.msg2.should eq(2)
end
- it "returns values in order to consecutive calls" do
- return_values = ["1",2,Object.new]
- @instance.stub(:msg).and_return(*return_values)
- @instance.msg.should == return_values[0]
- @instance.msg.should == return_values[1]
- @instance.msg.should == return_values[2]
- end
+ describe "#rspec_reset" do
+ it "removes stubbed methods that didn't exist" do
+ @instance.stub(:non_existent_method)
+ @instance.rspec_reset
+ @instance.should_not respond_to(:non_existent_method)
+ end
- it "keeps returning last value in consecutive calls" do
- return_values = ["1",2,Object.new]
- @instance.stub(:msg).and_return(return_values[0],return_values[1],return_values[2])
- @instance.msg.should == return_values[0]
- @instance.msg.should == return_values[1]
- @instance.msg.should == return_values[2]
- @instance.msg.should == return_values[2]
- @instance.msg.should == return_values[2]
- end
-
- it "reverts to original instance method if there is one" do
- @instance.existing_instance_method.should equal(:original_value)
- @instance.stub(:existing_instance_method).and_return(:mock_value)
- @instance.existing_instance_method.should equal(:mock_value)
- @instance.rspec_verify
- @instance.existing_instance_method.should equal(:original_value)
- end
+ it "restores existing instance methods" do
+ # See bug reports 8302 adn 7805
+ @instance.stub(:existing_instance_method) { :stub_value }
+ @instance.rspec_reset
+ @instance.existing_instance_method.should eq(:original_value)
+ end
- it "reverts to original private instance method if there is one" do
- @instance.existing_instance_method.should equal(:original_value)
- @instance.stub(:existing_private_instance_method).and_return(:mock_value)
- @instance.existing_instance_method.should equal(:mock_value)
- @instance.rspec_verify
- @instance.existing_instance_method.should equal(:original_value)
+ it "restores existing private instance methods" do
+ # See bug reports 8302 adn 7805
+ @instance.stub(:existing_private_instance_method) { :stub_value }
+ @instance.rspec_reset
+ @instance.send(:existing_private_instance_method).should eq(:original_value)
+ end
+
+ it "restores existing class methods" do
+ # See bug reports 8302 adn 7805
+ @class.stub(:existing_class_method) { :stub_value }
+ @class.rspec_reset
+ @class.existing_class_method.should eq(:original_value)
+ end
+
+ it "restores existing private class methods" do
+ # See bug reports 8302 adn 7805
+ @class.stub(:existing_private_class_method) { :stub_value }
+ @class.rspec_reset
+ @class.send(:existing_private_class_method).should eq(:original_value)
+ end
end
- it "reverts to original class method if there is one" do
- @class.existing_class_method.should equal(:original_value)
- @class.stub(:existing_class_method).and_return(:mock_value)
- @class.existing_class_method.should equal(:mock_value)
- @class.rspec_verify
- @class.existing_class_method.should equal(:original_value)
+ it "returns values in order to consecutive calls" do
+ @instance.stub(:msg).and_return("1",2,:three)
+ @instance.msg.should eq("1")
+ @instance.msg.should eq(2)
+ @instance.msg.should eq(:three)
end
- it "reverts to original private class method if there is one" do
- @class.existing_class_method.should equal(:original_value)
- @class.stub(:existing_private_class_method).and_return(:mock_value)
- @class.existing_class_method.should equal(:mock_value)
- @class.rspec_verify
- @class.existing_class_method.should equal(:original_value)
+ it "keeps returning last value in consecutive calls" do
+ @instance.stub(:msg).and_return("1",2,:three)
+ @instance.msg.should eq("1")
+ @instance.msg.should eq(2)
+ @instance.msg.should eq(:three)
+ @instance.msg.should eq(:three)
+ @instance.msg.should eq(:three)
end
it "yields a specified object" do
Please sign in to comment.
Something went wrong with that request. Please try again.