Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Backwards-update raise_error and cover all specs

  • Loading branch information...
commit 763f5df0e27adcf6ba6c9d66ee0538621656e0e2 1 parent 3909598
@txus authored
View
5 lib/stendhal/matchers.rb
@@ -17,13 +17,14 @@ def be_a(kind)
end
alias_method :be_kind_of, :be_a
alias_method :be_a_kind_of, :be_a
+ alias_method :be_an, :be_a
def include(member)
Inclusion.new(member)
end
- def raise_error(exception_class, message)
- RaiseError.new(exception_class, message)
+ def raise_error(*args)
+ RaiseError.new(*args)
end
def method_missing(m,*args)
View
2  lib/stendhal/matchers/raise_error.rb
@@ -2,7 +2,7 @@ module Stendhal
module Matchers
class RaiseError < AbstractMatcher
- def initialize(exception_class, message = nil)
+ def initialize(exception_class = Exception, message = nil)
if exception_class.new.is_a?(Exception)
@target = exception_class.new(message.to_s)
@message_given = !!message
View
16 spec/stendhal/matchers_spec.rb
@@ -17,13 +17,27 @@ module Stendhal
end
end
- describe "#be_a" do
+ describe "#be_a / #be_an" do
it 'creates a new kind expectation' do
Matchers::Kind.should_receive(:new).with(Fixnum)
object.be_a(Fixnum)
end
end
+ describe "#include" do
+ it 'creates a new inclusion expectation' do
+ Matchers::Inclusion.should_receive(:new).with(3)
+ object.include(3)
+ end
+ end
+
+ describe "#raise_error" do
+ it 'creates a new raise_error expectation' do
+ Matchers::RaiseError.should_receive(:new).with(StandardError)
+ object.raise_error(StandardError)
+ end
+ end
+
describe "#be_whatever" do
it 'creates a new predicate expectation' do
Matchers::Predicate.should_receive(:new).with(:frozen?)
View
2  spec/stendhal/mocks/mock_verifier_spec.rb
@@ -155,7 +155,7 @@ module Mocks
end
describe "#verify" do
- it 'registers a call for the given expectation' do
+ it 'verifies if times_called equals times_expected' do
subject.stub(:times_called).and_return 2
subject.stub(:times_expected).and_return 1
View
24 stendhal_spec/stendhal/example_group_spec.rb
@@ -13,13 +13,13 @@ module Stendhal
group.description.must eq("docstring")
end
- pending "allows example declaration inside the block" do
- expect {
+ it "allows example declaration inside the block" do
+ lambda {
ExampleGroup.new("docstring") do
it "does something" do
end
end
- }.to_not raise_error
+ }.must_not raise_error
end
describe "#add_example" do
@@ -77,6 +77,9 @@ module Stendhal
@pending_example.does_not_expect(:run)
@group.run
+
+ ExampleGroup.destroy_all
+ Example.destroy_all
end
it "runs all its children" do
@@ -101,6 +104,9 @@ module Stendhal
g.expects(:run)
end
@group.run
+
+ ExampleGroup.destroy_all
+ Example.destroy_all
end
it "returns an array with total examples, failures and pendings" do
@@ -122,6 +128,9 @@ module Stendhal
@group.add_example_group("another group")
@group.run.must eq([6,1,1])
+
+ ExampleGroup.destroy_all
+ Example.destroy_all
end
end
@@ -130,6 +139,9 @@ module Stendhal
example_group = ExampleGroup.new("group")
example_group.add_example_group(ExampleGroup.new("other group"))
example_group.example_groups.first.has_parent?.must eq(true)
+
+ ExampleGroup.destroy_all
+ Example.destroy_all
end
end
@@ -141,6 +153,9 @@ module Stendhal
ExampleGroup.new("group")
ExampleGroup.count.must eq(1)
+
+ ExampleGroup.destroy_all
+ Example.destroy_all
end
end
describe "#run_all" do
@@ -154,6 +169,9 @@ module Stendhal
group2.stubs(:run) { [1,0,1] }
ExampleGroup.run_all.must eq([2,2,4])
+
+ ExampleGroup.destroy_all
+ Example.destroy_all
end
end
end
View
20 stendhal_spec/stendhal/example_spec.rb
@@ -24,32 +24,32 @@ module Stendhal
example.run
end
- pending "captures unmet expectations" do
+ it "captures unmet expectations" do
example = Example.new("docstring") do
raise Stendhal::Exceptions::ExpectationNotMet.new("expected this example to be awesome")
end
- # expect {example.run}.to_not raise_error
+ lambda {example.run}.must_not raise_error
example.run
- example.should be_failed
- example.failed_message.should eq("expected this example to be awesome")
+ example.must be_failed
+ example.failed_message.must eq("expected this example to be awesome")
end
- pending "captures exceptions" do
+ it "captures exceptions" do
example = Example.new("docstring") do
raise "error"
end
- # expect {example.run}.to_not raise_error
+ lambda {example.run}.must_not raise_error
example.run
- example.should be_aborted
+ example.must be_aborted
end
- pending "captures everything else" do
+ it "captures everything else" do
example = Example.new("docstring") do
hello my dear reader
end
- # expect {example.run}.to_not raise_error
+ lambda {example.run}.must_not raise_error
example.run
- example.should be_aborted
+ example.must be_aborted
end
it "verifies all message expectations after running the block" do
View
5 stendhal_spec/stendhal/matchers/abstract_matcher_spec.rb
@@ -7,11 +7,6 @@ module Matchers
matcher.target.must eq("target")
end
- pending 'does not implement #match' do
- matcher = AbstractMatcher.new("target")
- # expect { matcher.match("whatever") }.to raise_error(NotImplementedError)
- end
-
end
end
end
View
12 stendhal_spec/stendhal/matchers/equality_spec.rb
@@ -8,12 +8,12 @@ module Matchers
matcher.match("string").must be_true
end
- pending 'fails when original and target objects are unequivalent' do
+ it 'fails when original and target objects are unequivalent' do
matcher = Equality.new("string")
expected_message = %q{expected "string", got "other string"}
- expect {
+ lambda {
matcher.match("other string")
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
context "with :negative => true" do
@@ -23,12 +23,12 @@ module Matchers
matcher.match("other string", :negative => true).must be_true
end
- pending 'fails otherwise' do
+ it 'fails otherwise' do
matcher = Equality.new("string")
expected_message = %q{expected something different than "string", got "string"}
- expect {
+ lambda {
matcher.match("string", :negative => true)
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
end
View
24 stendhal_spec/stendhal/matchers/inclusion_spec.rb
@@ -8,20 +8,20 @@ module Matchers
matcher.match([2, "string"]).must be_true
end
- pending 'fails when original does not respond to :include?' do
+ it 'fails when original does not respond to :include?' do
matcher = Inclusion.new(2)
expected_message = %q{expected 3 to include 2, but the former cannot include any object}
- expect {
+ lambda {
matcher.match(3)
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
- pending 'fails when original does not include the target' do
+ it 'fails when original does not include the target' do
matcher = Inclusion.new(2)
expected_message = %q{expected ["string"] to include 2}
- expect {
+ lambda {
matcher.match(["string"])
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
context "with :negative => true" do
@@ -31,20 +31,20 @@ module Matchers
matcher.match(["other string"], :negative => true).must be_true
end
- pending 'fails when original does not respond to :include?' do
+ it 'fails when original does not respond to :include?' do
matcher = Inclusion.new("string")
expected_message = %q{expected 3 not to include "string", but the former cannot include any object}
- expect {
+ lambda {
matcher.match(3, :negative => true)
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
- pending 'fails when original includes the target' do
+ it 'fails when original includes the target' do
matcher = Inclusion.new("string")
expected_message = %q{expected ["string"] not to include "string"}
- expect {
+ lambda {
matcher.match(["string"], :negative => true)
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
end
View
12 stendhal_spec/stendhal/matchers/kind_spec.rb
@@ -8,12 +8,12 @@ module Matchers
matcher.match(3).must be_true
end
- pending 'fails otherwise' do
+ it 'fails otherwise' do
matcher = Kind.new(Fixnum)
expected_message = %q{expected 3.2 to be a Fixnum}
- expect {
+ lambda {
matcher.match(3.2)
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
context "with :negative => true" do
@@ -23,12 +23,12 @@ module Matchers
matcher.match("string", :negative => true).must be_true
end
- pending 'fails otherwise' do
+ it 'fails otherwise' do
matcher = Kind.new(Numeric)
expected_message = %q{expected 3 not to be a Numeric}
- expect {
+ lambda {
matcher.match(3, :negative => true)
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
end
View
12 stendhal_spec/stendhal/matchers/predicate_spec.rb
@@ -8,12 +8,12 @@ module Matchers
matcher.match("string".freeze).must be_true
end
- pending 'fails when it returns false' do
+ it 'fails when it returns false' do
matcher = Predicate.new(:frozen?)
expected_message = %q{expected "string" to be frozen}
- expect {
+ lambda {
matcher.match("string")
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
context "with :negative => true" do
@@ -23,12 +23,12 @@ module Matchers
matcher.match("string", :negative => true).must be_true
end
- pending 'fails otherwise' do
+ it 'fails otherwise' do
matcher = Predicate.new(:frozen?)
expected_message = %q{expected "string" not to be frozen}
- expect {
+ lambda {
matcher.match("string".freeze, :negative => true)
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
end
end
View
135 stendhal_spec/stendhal/matchers/raise_error_spec.rb
@@ -0,0 +1,135 @@
+module Stendhal
+ module Matchers
+ describe RaiseError do
+
+ describe "#initialize" do
+ it "rejects arguments that aren't an Exception" do
+ lambda {
+ RaiseError.new(Hash)
+ }.must raise_error(Exception, "Hash cannot be raised")
+ end
+
+ it "assigns to @target an instance of the exception given" do
+ RaiseError.new(StandardError).target.must be_an(StandardError)
+ end
+
+ context "with a message provided" do
+
+ it "assigns to @target an instance of the exception given with a corresponding message" do
+ RaiseError.new(StandardError, "My message").target.message.must eq("My message")
+ end
+
+ end
+ end
+
+ describe "#match" do
+ it 'passes when original.call raises a given exception' do
+ blk = lambda {
+ raise StandardError.new
+ }
+ matcher = RaiseError.new(StandardError)
+ matcher.match(blk).must be_true
+ end
+
+ it 'fails when original.call raises a different exception' do
+ blk = lambda {
+ raise ArgumentError.new
+ }
+ matcher = RaiseError.new(NoMethodError)
+
+ expected_message = %q{expected block to raise NoMethodError, but raised #<ArgumentError: ArgumentError>}
+ lambda {
+ matcher.match(blk)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ end
+
+ it 'passes when original.call raises a given exception with a certain message' do
+ blk = lambda {
+ raise StandardError.new "My message"
+ }
+ matcher = RaiseError.new(StandardError, "My message")
+ matcher.match(blk).must be_true
+ end
+
+ it 'fails when original.call raises a given exception without a certain message' do
+ blk = lambda {
+ raise StandardError.new "Unexpected message"
+ }
+ matcher = RaiseError.new(StandardError, "My message")
+
+ expected_message = %q{expected block to raise #<StandardError: My message>, but raised #<StandardError: Unexpected message>}
+ lambda {
+ matcher.match(blk)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ end
+
+ it 'fails when original.call does not raise anything' do
+ blk = lambda {
+
+ }
+ matcher = RaiseError.new(StandardError)
+
+ expected_message = %q{expected block to raise StandardError, but nothing was raised}
+ lambda {
+ matcher.match(blk)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ end
+
+ context "with :negative => true" do
+
+ it 'fails when original.call raises a given exception' do
+ blk = lambda {
+ raise StandardError.new
+ }
+ matcher = RaiseError.new(StandardError)
+
+ expected_message = %q{expected block not to raise StandardError, but did}
+ lambda {
+ matcher.match(blk, :negative => true)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ end
+
+ it 'passes when original.call raises a different exception' do
+ blk = lambda {
+ raise ArgumentError.new
+ }
+ matcher = RaiseError.new(NoMethodError)
+
+ matcher.match(blk, :negative => true).must be_true
+ end
+
+ it 'fails when original.call raises a given exception with a certain message' do
+ blk = lambda {
+ raise StandardError.new "My message"
+ }
+ matcher = RaiseError.new(StandardError, "My message")
+
+ expected_message = %q{expected block not to raise #<StandardError: My message>, but did}
+ lambda {
+ matcher.match(blk, :negative => true)
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, expected_message)
+ end
+
+ it 'passes when original.call raises a given exception with a different message' do
+ blk = lambda {
+ raise StandardError.new "Unexpected message"
+ }
+ matcher = RaiseError.new(StandardError, "My message")
+ matcher.match(blk, :negative => true).must be_true
+ end
+
+ it 'passes when original.call does not raise anything' do
+ blk = lambda {
+
+ }
+ matcher = RaiseError.new(StandardError)
+
+ matcher.match(blk, :negative => true).must be_true
+ end
+
+ end
+ end
+
+ end
+ end
+end
View
16 stendhal_spec/stendhal/matchers_spec.rb
@@ -13,13 +13,27 @@ module Stendhal
end
end
- describe "#be_a" do
+ describe "#be_a / #be_an" do
it 'creates a new kind expectation' do
# Matchers::Kind.should_receive(:new).with(Fixnum)
MyClass.new.be_a(Fixnum).must be_a(Matchers::Kind)
end
end
+ describe "#include" do
+ it 'creates a new inclusion expectation' do
+ # Matchers::Inclusion.should_receive(:new).with(3)
+ MyClass.new.include(3).must be_a(Matchers::Inclusion)
+ end
+ end
+
+ describe "#raise_error" do
+ it 'creates a new raise_error expectation' do
+ # Matchers::RaiseError.should_receive(:new).with(StandardError)
+ MyClass.new.raise_error(StandardError).must be_a(Matchers::RaiseError)
+ end
+ end
+
describe "#be_whatever" do
it 'creates a new predicate expectation' do
# Matchers::Predicate.should_receive(:new).with(:frozen?)
View
12 stendhal_spec/stendhal/mocks/mock_verifier_spec.rb
@@ -168,13 +168,15 @@ module Mocks
end
describe "#verify" do
- pending 'registers a call for the given expectation' do
- subject.stub(:times_called).and_return 2
- subject.stub(:times_expected).and_return 1
+ it 'verifies if times_called equal times_expected' do
+ subject = MockVerifier::MessageExpectation.new(:length)
- expect {
+ subject.stubs(:times_called) { 2 }
+ subject.stubs(:times_expected) { 1 }
+
+ lambda {
subject.verify
- }.to raise_error(Stendhal::Exceptions::ExpectationNotMet, "expected to be sent :length 1 time, but received it 2 times")
+ }.must raise_error(Stendhal::Exceptions::ExpectationNotMet, "expected to be sent :length 1 time, but received it 2 times")
end
end
View
159 stendhal_spec/stendhal/mocks/mockable_spec.rb
@@ -0,0 +1,159 @@
+class MyArray < Array
+ include Stendhal::Mocks::Mockable
+end
+
+module Stendhal
+ module Mocks
+ describe "a Mockable class" do
+
+ describe "#expects" do
+ it 'creates a verifier' do
+ subject = MyArray.new([1,2,3])
+
+ subject.expects(:length)
+ subject.send(:__verifier).must be_a(MockVerifier)
+ end
+ it 'creates a message expectation on the given method' do
+ subject = MyArray.new([1,2,3])
+
+ subject.expects(:length)
+ subject.send(:__verifier).expectations.map(&:method).must include(:length)
+ end
+ it 'saves the original method' do
+ subject = MyArray.new([1,2,3])
+
+ subject.expects(:length)
+ subject.must respond_to(:__original_length)
+ subject.send(:__original_length).must eq(3)
+ end
+ it 'makes the mocked method register a call and call the original method afterwards' do
+ subject = MyArray.new([1,2,3])
+
+ subject.expects(:length)
+ subject.send(:__verifier).expects(:fulfill_expectation).once.ordered
+ subject.expects(:__original_length).once.ordered
+
+ subject.length
+ end
+ context "when the method is already mocked" do
+ it 'does not redefine it' do
+ subject = MyArray.new([1,2,3])
+
+ subject.stubs(:__original_length)
+ metaclass = (class << subject; self; end)
+ metaclass.does_not_expect(:alias_method)
+ metaclass.does_not_expect(:undef_method)
+ metaclass.does_not_expect(:class_eval)
+
+ subject.expects(:length)
+ end
+ end
+ it 'returns itself to allow chaining' do
+ subject = MyArray.new([1,2,3])
+
+ subject.expects(:length).must be_a(MyArray)
+ end
+ end
+
+ describe "times a method is expected to be received" do
+
+ {:once => 1,
+ :twice => 2}.each do |name,times|
+
+ describe "##{times}" do
+ pending 'sets the times expected for the mocked method to one' do
+ subject = MyArray.new([1,2,3])
+
+ expectation = double('expectation', :times_expected => 7)
+ subject.send(:__verifier).expects(:last_mocked_method).and_returns :length
+ subject.send(:__verifier).expects(:expectation_for).with(:length).and_returns expectation
+ expectation.expects(:times_expected=).with(times)
+
+ subject.send(name)
+ end
+ it 'returns itself to allow chaining' do
+ subject = MyArray.new([1,2,3])
+
+ subject.send(:__verifier).stubs(:last_mocked_method) { :length }
+ subject.expects(:length).send(name).must be_a(MyArray)
+ end
+ it 'raises an error if called without a previous mock' do
+ subject = MyArray.new([1,2,3])
+
+ lambda {
+ subject.send(name)
+ }.must raise_error(StandardError, "This object has no mocks.")
+ end
+ end
+
+ end
+
+ describe "#exactly(number_of_times)" do
+ pending 'sets the times expected for the mocked method to the given number' do
+ subject = MyArray.new([1,2,3])
+
+ expectation = double('expectation', :times_expected => 7)
+ subject.send(:__verifier).expects(:last_mocked_method).and_returns :length
+ subject.send(:__verifier).expects(:expectation_for).with(:length).and_returns expectation
+ expectation.expects(:times_expected=).with(3)
+
+ subject.exactly(3)
+ end
+ it 'returns itself to allow chaining' do
+ subject = MyArray.new([1,2,3])
+
+ subject.send(:__verifier).stubs(:last_mocked_method) { :length }
+ subject.expects(:length).twice.must be_a(MyArray)
+ end
+ it 'raises an error if called without a previous mock' do
+ subject = MyArray.new([1,2,3])
+
+ lambda {
+ subject.exactly(2)
+ }.must raise_error(StandardError, "This object has no mocks.")
+ end
+ end
+
+ describe "#times" do
+ it 'returns itself to allow chaining' do
+ subject = MyArray.new([1,2,3])
+
+ subject.expects(:length).times.must be_a(MyArray)
+ end
+ it 'raises an error if called without a previous mock' do
+ subject = MyArray.new([1,2,3])
+
+ lambda {
+ subject.times
+ }.must raise_error(StandardError, "This object has no mocks.")
+ end
+ end
+
+ end
+
+ describe "#and_returns" do
+ it 'stubs the return value' do
+ subject = MyArray.new([1,2,3])
+
+ subject.expects(:length).and_returns 5
+ subject.length.must eq(5)
+ end
+ it 'accepts a block as the return value' do
+ subject = MyArray.new([1,2,3])
+
+ subject.expects(:length).and_returns do
+ 3 + 4
+ end
+ subject.length.must eq(7)
+ end
+ it 'saves the unstubbed method' do
+ subject = MyArray.new([1,2,3])
+
+ subject.expects(:length).and_returns(:foo)
+ subject.must respond_to(:__unstubbed_length)
+ subject.send(:__unstubbed_length).must eq(3)
+ end
+ end
+ end
+ end
+end
View
41 stendhal_spec/stendhal/mocks/stubbable_spec.rb
@@ -0,0 +1,41 @@
+class MyArray < Array
+ include Stendhal::Mocks::Stubbable
+end
+
+module Stendhal
+ module Mocks
+ describe "a Stubbable class" do
+
+ describe "#stubs" do
+ it 'stubs an existing method and makes it return nil' do
+ subject = MyArray.new([1,2,3])
+
+ subject.stubs(:length)
+ subject.length.must be_nil
+ end
+ it 'stubs an unexisting method and makes it return nil' do
+ subject = MyArray.new([1,2,3])
+
+ subject.stubs(:w00t)
+ subject.w00t.must be_nil
+ end
+ it 'saves the unstubbed method' do
+ subject = MyArray.new([1,2,3])
+
+ subject.stubs(:length)
+ subject.respond_to?(:__unstubbed_length).must be_true
+ subject.send(:__unstubbed_length).must eq(3)
+ end
+ context "with a block" do
+ it 'makes the stub return the result of the block' do
+ subject = MyArray.new([1,2,3])
+
+ subject.stubs(:w00t) { 3 + 4 }
+ subject.w00t.must eq(7)
+ end
+ end
+ end
+
+ end
+ end
+end
View
37 stendhal_spec/stendhal/mocks/test_double_spec.rb
@@ -0,0 +1,37 @@
+module Stendhal
+ module Mocks
+ describe TestDouble do
+
+ it 'is created with a name' do
+ subject = TestDouble.new('double')
+
+ subject.name.must eq('double')
+ end
+
+ context "when given a hash of stubs" do
+
+ it 'assigns them to the test double' do
+ subject = TestDouble.new('double', :my_stub => 3)
+
+ subject.respond_to?(:my_stub).must be_true
+ end
+
+ it 'makes every stub return its value' do
+ subject = TestDouble.new('double', :my_stub => 3)
+
+ subject.my_stub.must eq(3)
+ end
+
+ context "when giving arguments to the stubbed method" do
+ it 'ignores them' do
+ subject = TestDouble.new('double', :my_stub => 3)
+
+ subject.my_stub("argument").must eq(3)
+ end
+ end
+
+ end
+
+ end
+ end
+end

0 comments on commit 763f5df

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