Skip to content

Commit

Permalink
Merge pull request #19 from jdenen/master
Browse files Browse the repository at this point in the history
support compounding expectations
  • Loading branch information
JonRowe committed Nov 6, 2014
2 parents faf65b0 + 974dcf8 commit b6ce674
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rspec/collection_matchers/have.rb
@@ -1,6 +1,8 @@
module RSpec
module CollectionMatchers
class Have
include RSpec::Matchers::Composable unless RSpec::Core::Version::STRING < '3.0'

QUERY_METHODS = [:size, :length, :count].freeze
IGNORED_CLASSES = [Integer].freeze

Expand Down
46 changes: 46 additions & 0 deletions spec/rspec/collection_matchers/have_spec.rb
Expand Up @@ -384,6 +384,52 @@ def items
end
end

describe "expectations compounded with RSpec::Matchers::Composable", :if => defined?(RSpec::Matchers::Composable) do
context "using #and" do
it "fails with relevant error when only first expectation fails" do
expect {
expect([1, 2, 3]).to be_falsey.and have(3).items
}.to fail_matching "expected: falsey value"
end

it "fails with relevant error when only second expectation fails" do
expect {
expect([1, 2, 3]).to have(3).items.and be_falsey
}.to fail_matching "expected: falsey value"
end

it "fails with relevant error when both expectations fail" do
expect {
expect([1, 2, 3]).to be_falsey.and have(0).items
}.to fail_matching "...and:"
end

it "passes when both expectations are met" do
expect([1, 2, 3]).to have(3).items.and be_truthy
end
end

context "using #or" do
it "fails with relevant error when neither expectation is met" do
expect {
expect([1, 2, 3]).to be_falsey.or have(0).items
}.to fail_matching "...or:"
end

it "passes when only first expectation is met" do
expect([1, 2, 3]).to have(3).items.or be_falsey
end

it "passes when only second expectation is met" do
expect([1, 2, 3]).to be_falsey.or have(3).items
end

it "passes when both expectations are met" do
expect([1, 2, 3]).to be_truthy.or have(3).items
end
end
end

describe RSpec::CollectionMatchers::Have, "for a collection owner that implements #send" do
before(:each) do
@collection = Object.new
Expand Down

0 comments on commit b6ce674

Please sign in to comment.