Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Classes don't follow the double docs WRT #dup #1586

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions spec/rspec/mocks/partial_double_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ def self.inspect
expect { duplicate.foobar }.to raise_error(NoMethodError, /foobar/)
expect { verify object }.to fail_with(/foobar/)
end

describe "class" do
let(:object) do
Class.new
end

it "shares message expectations with clone" do
expect(object).to receive(:foobar)
twin = object.clone
twin.foobar
expect { verify twin }.not_to raise_error
expect { verify object }.not_to raise_error
end

it "clears message expectations when `dup`ed" do
expect(object).to receive(:foobar)
duplicate = object.dup
expect { duplicate.foobar }.to raise_error(NoMethodError, /foobar/)
expect { verify object }.to fail_with(/foobar/)
end
end
end

RSpec.describe "Using a reopened file object as a partial mock" do
Expand Down
12 changes: 12 additions & 0 deletions spec/rspec/mocks/stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ def existing_private_instance_method
expect { @class.existing_private_class_method }.to raise_error NoMethodError, /private method [`']existing_private_class_method/
end

context "on a class" do
it "is retained when stubbed object is `clone`d" do
allow(@class).to receive(:foobar).and_return(1)
expect(@class.clone.foobar).to eq(1)
end

it "is cleared when stubbed object when `dup`ed" do
allow(@class).to receive(:foobar).and_return(1)
expect { @class.dup.foobar }.to raise_error NoMethodError, /foobar/
end
end

context "using `with`" do
it 'determines which value is returned' do
allow(@stub).to receive(:foo).with(1) { :one }
Expand Down
Loading