Permalink
Browse files

Add expect to example_methods

- expect { this_block }.to change{this}.from(this).to(that)
- expect { this_block }.to raise_error
  • Loading branch information...
1 parent 552d027 commit 7e4f872b4becbd41588da95c0e5d954a6e770293 @dchelimsky committed Apr 22, 2009
View
@@ -4,6 +4,8 @@
* name argument to mock/stub is now optional (closes #782)
* you can do mock(:foo => "woo", :bar => "car")
+ * expect { this_block }.to change(this).from(this).to(that)
+ * expect { this_block }.to raise_error
* bug fixes
@@ -0,0 +1,65 @@
+Feature: expect change
+
+ Expect some code (wrapped in a proc) to change the state of some object.
+
+ Scenario: expecting change
+ Given a file named "expect_change.rb" with:
+ """
+ class Counter
+ class << self
+ def increment
+ @count ||= 0
+ @count += 1
+ end
+
+ def count
+ @count ||= 0
+ end
+ end
+ end
+
+ describe Counter, "#increment" do
+ it "should increment the count" do
+ expect{Counter.increment}.to change{Counter.count}.from(0).to(1)
+ end
+
+ # deliberate failure
+ it "should increment the count by 2" do
+ expect{Counter.increment}.to change{Counter.count}.by(2)
+ end
+ end
+ """
+ When I run "spec expect_change.rb"
+ Then the stdout should match "2 examples, 1 failure"
+ Then the stdout should match "should have been changed by 2, but was changed by 1"
+
+ Scenario: expecting no change
+ Given a file named "expect_no_change.rb" with:
+ """
+ class Counter
+ class << self
+ def increment
+ @count ||= 0
+ @count += 1
+ end
+
+ def count
+ @count ||= 0
+ end
+ end
+ end
+
+ describe Counter, "#increment" do
+ it "should not increment the count by 2" do
+ expect{Counter.increment}.to_not change{Counter.count}.from(0).to(2)
+ end
+
+ # deliberate failure
+ it "should not increment the count by 1" do
+ expect{Counter.increment}.to_not change{Counter.count}.by(1)
+ end
+ end
+ """
+ When I run "spec expect_no_change.rb"
+ Then the stdout should match "2 examples, 1 failure"
+ Then the stdout should match "should not have changed, but did change from 1 to 2"
@@ -0,0 +1,44 @@
+Feature: expect error
+
+ Expect a proc to change the state of some object.
+
+ Scenario: expect error
+ Given a file named "expect_error.rb" with:
+ """
+ describe Object, "#non_existent_message" do
+ it "should raise" do
+ expect{Object.non_existent_message}.to raise_error(NameError)
+ end
+ end
+
+ #deliberate failure
+ describe Object, "#public_instance_methods" do
+ it "should raise" do
+ expect{Object.public_instance_methods}.to raise_error(NameError)
+ end
+ end
+ """
+ When I run "spec expect_error.rb"
+ Then the stdout should match "2 examples, 1 failure"
+ Then the stdout should match "expected NameError but nothing was raised"
+
+ Scenario: expect no error
+ Given a file named "expect_no_error.rb" with:
+ """
+ describe Object, "#public_instance_methods" do
+ it "should not raise" do
+ expect{Object.public_instance_methods}.to_not raise_error(NameError)
+ end
+ end
+
+ #deliberate failure
+ describe Object, "#non_existent_message" do
+ it "should not raise" do
+ expect{Object.non_existent_message}.to_not raise_error(NameError)
+ end
+ end
+ """
+ When I run "spec expect_no_error.rb"
+ Then the stdout should match "2 examples, 1 failure"
+ Then the stdout should match "undefined method `non_existent_message'"
+
@@ -52,6 +52,15 @@ def execute(run_options, instance_variables) # :nodoc:
run_options.reporter.example_finished(@_proxy.update(description), execution_error)
success = execution_error.nil? || ExamplePendingError === execution_error
end
+
+ module Aliases
+ alias_method :to, :should
+ alias_method :to_not, :should_not
+ end
+
+ def expect(&block)
+ block.extend Aliases
+ end
def eval_each_fail_fast(blocks) # :nodoc:
blocks.each {|block| instance_eval(&block)}
@@ -117,6 +117,18 @@ def module_that_is_reopened_method; end
end.should raise_error(/No description supplied for example declared on this backtrace/)
end
end
+
+ describe "#expect" do
+ it "aliases #should with #to on the proc" do
+ a = 3
+ expect { a += 1 }.to change{a}.from(3).to(4)
+ end
+
+ it "aliases #should_not with #to_not on the proc" do
+ a = 3
+ expect { nil }.to_not change{a}
+ end
+ end
end
end

0 comments on commit 7e4f872

Please sign in to comment.