class FuzzyProxy < BasicObject def initialize(target) @target = target end ! def fuzzy? true end ! def method_missing(*args, &block) @target.__send__(*args, &block) end end
class CustomFormatter # This registers the notifications this formatter # supports, and tells RSpec::Core::Formatters.register self, :example_sta ! def initialize(output) @output = output end ! def example_started(notification) @output << "example: “ @output << notification.example.description end ! end
require ‘rspec/its' ! describe do ! person = Struct.new(:first, :last) do def name [first, last].join(' ') end end ! subject(:bob) { person.new('Bob', 'Jones') } ! its(:name) { is_expected.to eq 'Bob Jones' } ! end
examples$ rspec lies_spec.rb --format doc ! ! name should eq "Bob Jones" ! Finished in 0.00094 seconds (files took 0.09512 1 example, 0 failures examples$
require ‘rspec/its' ! describe "#name" do ! person = Struct.new(:first, :last) do def name [first, last].join(' ') end end ! it "combines first and last" do bob = person.new('Bob', 'Jones') expect(bob.name).to eq 'Bob Jones' end ! end
examples$ rspec refactor_spec.rb --format doc ! ! name combines first and last ! Finished in 0.00084 seconds (files took 0.0084 1 example, 0 failures examples$
describe "verified doubles" do ! it "does something" do my_obj = instance_double( "MyObject", does: "cool things” ) expect(my_obj.does).to eq "cool things" end ! end
describe "verified doubles" do ! class MyObject end ! it "does something" do my_obj = instance_double( "MyObject", does: "cool things” ) expect(my_obj.does).to eq "cool things" end end
examples$ rspec verified_doubles_in_situ_spe F ! Failures: ! 1) verified doubles does something Failure/Error: my_obj = instance_double("M MyObject does not implement: does # ./verified_doubles_in_situ_spec.rb:7:in ! Finished in 0.00046 seconds (files took 0.07 1 example, 1 failure
describe "verified doubles" do ! class MyObject def does; true; end end ! it "does something" do my_obj = instance_double( "MyObject", does: "cool things” ) expect(my_obj.does).to eq "cool things" end end
describe "verified doubles" do ! class MyObject def does; true; end end ! it "does something" do my_obj = MyObject.new allow(my_obj).to receive(:other) { "things" } expect(my_obj.other).to eq "things" end end
describe "verified doubles" do ! class MyObject def does; true; end end ! RSpec.configuration.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end ! it "does something" do my_obj = MyObject.new allow(my_obj).to receive(:other) { "things" } expect(my_obj.other).to eq "things" end end
examples$ rspec verify_partial_doubles_spec. F ! Failures: ! 1) verified doubles does something Failure/Error: allow(my_obj).to receive(:o # does not implement: other # ./verify_partial_doubles_spec.rb:13:in ! Finished in 0.00562 seconds (files took 0.08 1 example, 1 failure
examples$ rspec broken_let_spec.rb F ! Failures: ! 1) Failure/Error: thing RuntimeError: let declaration `thing` accessed in a `before(:context)` hook at: examples/brok `let` and `subject` declarations are not intended to be called in a `before(:conte hook, as they exist to define state that is reset between each example, while
examples$ rspec before_all_stubs_broken_spec F ! Failures: ! 1) Failure/Error: allow(Object).to receive The use of doubles or partial doubles from rspec-mocks outside of the per-test lifecycle is not supported. # ./before_all_stubs_broken_spec.rb:4:in ! Finished in 0.00504 seconds (files took 0.07 1 example, 1 failure
describe do ! subject(:string) { "a string" } ! it { should be_a String } its(:length) { should == 8 } ! specify do string.should == "a string" end ! end
describe do ! subject(:string) { "a string" } ! it { is_expected.to be_a String } ! describe '#length' do subject { super().length } it { is_expected.to eq(8) } end ! specify do expect(string).to eq("a string") end ! end