Skip to content
Closed
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
2 changes: 1 addition & 1 deletion features/its.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Feature: attribute of subject
person
end

its("phone_numbers.first") { should eq("555-1212") }
its("phone_numbers.first") { is_expected.to eq("555-1212") }
end
end
"""
Expand Down
18 changes: 18 additions & 0 deletions lib/rspec/its.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
require 'rspec/its/version'
require 'rspec/core'

# RSpec.deprecate <<EOS
# rspec-its deprecates calling _send_ in favour of _public_send_ internally.
# This change will be introduced with version 2.0.
# Maybe you're testing private methods in your test suite without your intention.
# If you need more information about which test is affected,
# set `config.its_raise_errors_for_private_method_calling = true`
# EOS

module RSpec
module Its

Expand Down Expand Up @@ -131,6 +139,11 @@ def its(attribute, *options, &block)
else
attribute_chain = attribute.to_s.split('.')
attribute_chain.inject(subject) do |inner_subject, attr|

if inner_subject.private_methods(false).include?(attr.to_sym)
RSpec.deprecate("Testing private method #{attr}", :call_site => its_caller.first)
end

inner_subject.send(attr)
end
end
Expand Down Expand Up @@ -175,6 +188,11 @@ def should_not(matcher=nil, message=nil)
end

RSpec.configure do |rspec|

# Add RSpec configuration setting to allow
# the user to handle private method invoking warning
rspec.add_setting :its_raise_errors_for_private_method_calling
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping in mind there's configuration.raise_errors_for_deprecations! I'm on the fence if we need this setting, or a deprecation message will be sufficient.


rspec.extend RSpec::Its
rspec.backtrace_exclusion_patterns << %r(/lib/rspec/its)
end
Expand Down
38 changes: 38 additions & 0 deletions spec/rspec/its_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
require 'spec_helper'

# class RSpec::Core::Formatters::DeprecationFormatter
# Formatters.register self, :baem

# def baem(notification)
# output << 'baem'
# end
# end
module RSpec
describe Its do
describe "#its" do
Expand All @@ -9,6 +16,37 @@ module RSpec
its([]) { expect(described_class).to be Its }
end
end

context "raises error when calling a private method" do
subject do
Class.new do
private

def name
'Maria'
end

def self.name
'Maria'
end
private_class_method :name
end
end

before(:each) do
# expect(RSpec).to receive(:deprecate)
# .with('Testing private method name is deprecated', anything)
end

context 'on an instance' do
its('new.name') { should eq('Maria') }
end

context 'on a class' do
its('name') { should eq('Maria') }
end
end

context "with explicit subject" do
subject do
Class.new do
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ def method_missing(method, *args, &block)
RSpec.configure do |config|
config.run_all_when_everything_filtered = true
config.order = 'random'

# config.raise_errors_for_deprecations!

# config.its_private_method_debug = true
end