1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
RSpec.describe "expect(...).to satisfy { block }" do
it_behaves_like "an RSpec matcher", :valid_value => true, :invalid_value => false do
let(:matcher) { satisfy { |v| v } }
end
it "describes itself" do
expect(satisfy.description).to eq("satisfy block")
end
it "passes if block returns true" do
expect(true).to satisfy { |val| val }
expect(true).to satisfy do |val|
val
end
end
context "when no custom description is provided" do
context 'in Ripper supported environment', :if => RSpec::Support::RubyFeatures.ripper_supported? do
it "fails with block snippet if block returns false" do
expect {
expect(false).to satisfy { |val| val }
}.to fail_with("expected false to satisfy expression `val`")
expect do
expect(false).to satisfy do |val|
val
end
end.to fail_with("expected false to satisfy expression `val`")
end
context 'when used with an alias name' do
alias_matcher :fulfill, :satisfy
it 'can extract the block snippet' do
expect {
expect(false).to fulfill { |val| val }
}.to fail_with("expected false to fulfill expression `val`")
end
end
end
context 'in Ripper unsupported environment', :unless => RSpec::Support::RubyFeatures.ripper_supported? do
it "fails without block snippet if block returns false" do
expect {
expect(false).to satisfy { |val| val }
}.to fail_with("expected false to satisfy block")
expect do
expect(false).to satisfy do |val|
val
end
end.to fail_with("expected false to satisfy block")
end
end
end
context "when a custom description is provided" do
it "describes itself" do
expect(satisfy("be awesome").description).to eq("be awesome")
end
it "passes if block returns true" do
expect(true).to satisfy("be true") { |val| val }
expect(true).to satisfy("be true") do |val|
val
end
end
it "fails with the custom description if block returns false" do
expect {
expect(false).to satisfy("be true") { |val| val }
}.to fail_with("expected false to be true")
expect do
expect(false).to satisfy("be true") do |val|
val
end
end.to fail_with("expected false to be true")
end
end
end
RSpec.describe "expect(...).not_to satisfy { block }" do
it "passes if block returns false" do
expect(false).not_to satisfy { |val| val }
expect(false).not_to satisfy do |val|
val
end
end
context "when no custom description is provided" do
context 'in Ripper supported environment', :if => RSpec::Support::RubyFeatures.ripper_supported? do
it "fails with block snippet if block returns true" do
expect {
expect(true).not_to satisfy { |val| val }
}.to fail_with("expected true not to satisfy expression `val`")
end
end
context 'in Ripper unsupported environment', :unless => RSpec::Support::RubyFeatures.ripper_supported? do
it "fails without block snippet if block returns true" do
expect {
expect(true).not_to satisfy { |val| val }
}.to fail_with("expected true not to satisfy block")
end
end
end
context "when a custom description is provided" do
it "passes if block returns false" do
expect(false).not_to satisfy("be true") { |val| val }
expect(false).not_to satisfy("be true") do |val|
val
end
end
it "fails with the custom description if block returns true" do
expect {
expect(true).not_to satisfy("be true") { |val| val }
}.to fail_with("expected true not to be true")
end
end
end
|