spec/rspec/its_spec.rb in rspec-its-1.2.0 vs spec/rspec/its_spec.rb in rspec-its-1.3.0

- old
+ new

@@ -53,20 +53,28 @@ end end.new end its("name") { should eq("John") } its("name.size") { should eq(4) } - its("name.size.class") { should eq(Fixnum) } + if RUBY_VERSION >= "2.4.0" + its("name.size.class") { should eq(Integer) } + else + its("name.size.class") { should eq(Fixnum) } + end context "using should_not" do its("name") { should_not eq("Paul") } end context "using is_expected" do its("name") { is_expected.to eq("John") } end + context "using will_not" do + its("name") { will_not raise_error } + end + context "using are_expected" do its("name.chars.to_a") { are_expected.to eq(%w[J o h n]) } end end @@ -84,19 +92,27 @@ end end.new end its([:a]) { should eq("Symbol: a") } its(['a']) { should eq("String: a") } - its([:b, 'c', 4]) { should eq("Symbol: b; String: c; Fixnum: 4") } + if RUBY_VERSION >= "2.4.0" + its([:b, 'c', 4]) { should eq("Symbol: b; String: c; Integer: 4") } + else + its([:b, 'c', 4]) { should eq("Symbol: b; String: c; Fixnum: 4") } + end its(:name) { should eq("George") } context "when referring to an attribute that doesn't exist" do context "it raises an error" do its(:age) do expect do should eq(64) end.to raise_error(NoMethodError) end + + context "using will" do + its(:age) { will raise_error(NoMethodError) } + end end end context "when it's a hash" do subject { {:a => {:deep => {:key => "value"}}} } @@ -107,10 +123,14 @@ context "when referring to a key that doesn't exist" do its([:not_here]) { should be_nil } its([:a, :ghost]) { should be_nil } its([:deep, :ghost]) { expect { should eq("missing") }.to raise_error(NoMethodError) } + + context "using will" do + its([:deep, :ghost]) { will raise_error(NoMethodError) } + end end end end context "when it does not respond to #[]" do @@ -120,10 +140,14 @@ its([:a]) do expect do should eq("Symbol: a") end.to raise_error(NoMethodError) end + + context "using will" do + its([:a]) { will raise_error(NoMethodError) } + end end end context "calling and overriding super" do it "calls to the subject defined in the parent group" do @@ -228,9 +252,128 @@ context "preserves access to metadata that ends in hash" do its([], :foo, :bar => 17) do |example| expect(example.metadata[:foo]).to be(true) expect(example.metadata[:bar]).to be(17) end + end + end + + context "when expecting errors" do + subject do + Class.new do + def good; end + + def bad + raise ArgumentError, "message" + end + end.new + end + + its(:good) { will_not raise_error } + its(:bad) { will raise_error } + its(:bad) { will raise_error(ArgumentError) } + its(:bad) { will raise_error("message") } + its(:bad) { will raise_error(ArgumentError, "message") } + end + + context "when expecting throws" do + subject do + Class.new do + def good; end + + def bad + throw :abort, "message" + end + end.new + end + + its(:good) { will_not throw_symbol } + its(:bad) { will throw_symbol } + its(:bad) { will throw_symbol(:abort) } + its(:bad) { will throw_symbol(:abort, "message") } + end + + context "with change observation" do + subject do + Class.new do + attr_reader :count + + def initialize + @count = 0 + end + + def increment + @count += 1 + end + + def noop; end + end.new + end + + its(:increment) { will change { subject.count }.by(1) } + its(:increment) { will change { subject.count }.from(0) } + its(:increment) { will change { subject.count }.from(0).to(1) } + its(:increment) { will change { subject.count }.by_at_least(1) } + its(:increment) { will change { subject.count }.by_at_most(1) } + + its(:noop) { will_not change { subject.count } } + its(:noop) { will_not change { subject.count }.from(0) } + + its(:increment) do + expect { will_not change { subject.count }.by(0) }.to \ + raise_error(NotImplementedError, '`expect { }.not_to change { }.by()` is not supported') + end + + its(:increment) do + expect { will_not change { subject.count }.by_at_least(2) }.to \ + raise_error(NotImplementedError, '`expect { }.not_to change { }.by_at_least()` is not supported') + end + + its(:increment) do + expect { will_not change { subject.count }.by_at_most(3) }.to \ + raise_error(NotImplementedError, '`expect { }.not_to change { }.by_at_most()` is not supported') + end + end + + context "with output capture" do + subject do + Class.new do + def stdout + print "some output" + end + + def stderr + $stderr.print "some error" + end + + def noop; end + end.new + end + + its(:stdout) { will output("some output").to_stdout } + its(:stderr) { will output("some error").to_stderr } + + its(:noop) { will_not output("some error").to_stderr } + its(:noop) { will_not output("some output").to_stdout } + end + + context "#will with non block expectations" do + subject do + Class.new do + def terminator + "back" + end + end.new + end + + its(:terminator) do + expect { will be("back") }.to \ + raise_error(ArgumentError, '`will` only supports block expectations') + end + + its(:terminator) do + expect { will_not be("back") }.to \ + raise_error(ArgumentError, '`will_not` only supports block expectations') end end end end end