Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update the yield cukes with a few more/better examples.

  • Loading branch information...
commit d020cfe51d0733821415d9a0036f583048cff41e 1 parent f8c3c01
@myronmarston myronmarston authored
Showing with 55 additions and 29 deletions.
  1. +55 −29 features/built_in_matchers/yield.feature
View
84 features/built_in_matchers/yield.feature
@@ -22,16 +22,35 @@ Feature: yield matchers
to detect whether or not your method yields, and, if so, how many times and what
the yielded arguments are.
+ Background:
+ Given a file named "my_class.rb" with:
+ """
+ class MyClass
+ def self.yield_once_with(*args)
+ yield *args
+ end
+
+ def self.raw_yield
+ yield
+ end
+
+ def self.dont_yield
+ end
+ end
+ """
+
Scenario: yield_control matcher
Given a file named "yield_control_spec.rb" with:
"""
+ require './my_class'
+
describe "yield_control matcher" do
- specify { expect { |b| File.open("temp", "w", &b) }.to yield_control }
- specify { expect { |b| :foo.to_s(&b) }.not_to yield_control }
+ specify { expect { |b| MyClass.yield_once_with(1, &b) }.to yield_control }
+ specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_control }
# deliberate failures
- specify { expect { |b| :foo.to_s(&b) }.to yield_control }
- specify { expect { |b| File.open("temp", "w", &b) }.not_to yield_control }
+ specify { expect { |b| MyClass.yield_once_with(1, &b) }.not_to yield_control }
+ specify { expect { |b| MyClass.dont_yield(&b) }.to yield_control }
end
"""
When I run `rspec yield_control_spec.rb`
@@ -43,51 +62,58 @@ Feature: yield matchers
Scenario: yield_with_args matcher
Given a file named "yield_with_args_spec.rb" with:
"""
+ require './my_class'
+
describe "yield_with_args matcher" do
- specify { expect { |b| File.open("temp", "w", &b) }.to yield_with_args }
- specify { expect { |b| File.open("temp", "w", &b) }.to yield_with_args(File) }
- specify { expect { |b| "Foo 17".sub(/\d+/, &b) }.to yield_with_args("17") }
- specify { expect { |b| "Foo 17".sub(/\d+/, &b) }.to yield_with_args(/1\d/) }
+ specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args }
+ specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args("foo") }
+ specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(String) }
+ specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(/oo/) }
+
+ specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args("foo", "bar") }
+ specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(String, String) }
+ specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(/fo/, /ar/) }
+
+ specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args(17, "baz") }
# deliberate failures
- specify { expect { |b| :foo.to_s(&b) }.to yield_with_args(3, 4) }
- specify { expect { |b| "Foo 17".sub(/\d+/, &b) }.to yield_with_args("18") }
- specify { expect { |b| File.open("temp", "w", &b) }.not_to yield_with_args(File) }
- specify { expect { |b| File.open("temp", "w", &b) }.not_to yield_with_args }
+ specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args }
+ specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args("foo") }
+ specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(String) }
+ specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(/oo/) }
+ specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args("foo", "bar") }
+ specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(17, "baz") }
end
"""
When I run `rspec yield_with_args_spec.rb`
Then the output should contain all of these:
- | 8 examples, 4 failures |
- | expected given block to yield with arguments, but did not yield |
- | expected given block to yield with arguments, but yielded with unexpected arguments |
- | expected given block not to yield with arguments, but yielded with expected arguments |
+ | 14 examples, 6 failures |
| expected given block not to yield with arguments, but did |
+ | expected given block not to yield with arguments, but yielded with expected arguments |
+ | expected given block to yield with arguments, but yielded with unexpected arguments |
Scenario: yield_with_no_args matcher
Given a file named "yield_with_no_args_spec.rb" with:
"""
- def raw_yield
- yield
- end
+ require './my_class'
describe "yield_with_no_args matcher" do
- specify { expect { |b| raw_yield(&b) }.to yield_with_no_args }
- specify { expect { |b| :foo.to_s(&b) }.not_to yield_with_no_args }
- specify { expect { |b| File.open("temp", "w", &b) }.not_to yield_with_no_args }
+ specify { expect { |b| MyClass.raw_yield(&b) }.to yield_with_no_args }
+ specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_with_no_args }
+ specify { expect { |b| MyClass.yield_once_with("a", &b) }.not_to yield_with_no_args }
# deliberate failures
- specify { expect { |b| raw_yield(&b) }.not_to yield_with_no_args }
- specify { expect { |b| :foo.to_s(&b) }.to yield_with_no_args }
- specify { expect { |b| File.open("temp", "w", &b) }.to yield_with_no_args }
+ specify { expect { |b| MyClass.raw_yield(&b) }.not_to yield_with_no_args }
+ specify { expect { |b| MyClass.dont_yield(&b) }.to yield_with_no_args }
+ specify { expect { |b| MyClass.yield_once_with("a", &b) }.to yield_with_no_args }
end
"""
When I run `rspec yield_with_no_args_spec.rb`
Then the output should contain all of these:
- | 6 examples, 3 failures |
- | expected given block not to yield with no arguments, but did |
- | expected given block to yield with no arguments, but did not yield |
- | expected given block to yield with no arguments, but yielded with arguments: [#<File:temp (closed)>] |
+ | 6 examples, 3 failures |
+ | expected given block not to yield with no arguments, but did |
+ | expected given block to yield with no arguments, but did not yield |
+ | expected given block to yield with no arguments, but yielded with arguments: ["a"] |
Scenario: yield_successive_args matcher
Given a file named "yield_successive_args_spec.rb" with:

0 comments on commit d020cfe

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