Hi Arup You need to define `expected` and `actual` to surface the variables you wish to diff. In your case `expected` should be `@second` and `actual` should be `owner.const_defined?(const) && owner.const_get(const)`.
See the matcher protocol documentation for more information. http://rspec.info/documentation/3.8/rspec-expectations/RSpec/Matchers/MatcherProtocol.html#diffable%3F-instance_method Cheers Jon Rowe --------------------------- [email protected] jonrowe.co.uk On 13 May 2019 at 10:56, Arup Rakshit wrote: > Hi, > > I am trying to write a custom matcher, but not able to show the diff and > error message correctly. I need help with it. > > RSpec::Matchers.define :have_constant do |const| > match do |owner| > owner.const_defined?(const) && owner.const_get(const) == @second > end > > chain :with_value do |second| > @second = second > end > > diffable > > failure_message do |actual| > "expected that #{const} would be defined in #{actual} with #{@second}" > end > end > > > And the spec is: > > describe "FeatureService" do > describe 'constants' do > subject { FeatureService } > > it { is_expected.to (http://expected.to) > have_constant(:IMO_NUMBER_VALIDATION).with_value('6cjXBWdq') } > it { is_expected.to (http://expected.to) > have_constant(:JOB_TYPES).with_value('6cjXBWdq') } > # ... > end > end > > > The actual class is: > > class FeatureService > JOB_TYPES = 'WN2HsoIm'.freeze > > #... > end > > > When I run the test the failure message is not quiet helpful. So how can I > tweak the custom matcher so that it can be helpful when it writes to stdout > than what I see currently. > > $ rspec ./spec/services/feature_service_spec.rb:8 > Run options: include > {:locations=>{"./spec/services/feature_service_spec.rb"=>[8]}} > F > > Failures: > > 1) FeatureService constants should have constant :JOB_TYPES with value > "6cjXBWdq" > Failure/Error: it { is_expected.to (http://expected.to) > have_constant(:JOB_TYPES).with_value('6cjXBWdq') } > > expected that JOB_TYPES would be defined in FeatureService with 6cjXBWdq > Diff: > @@ -1,2 +1,2 @@ > -:JOB_TYPES > +FeatureService > > # ./spec/services/feature_service_spec.rb:8:in `block (3 levels) in <top > (required)>' > > > > > > > -- You received this message because you are subscribed to the Google Groups "rspec" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/dejalu-217-a266d09c-960f-47ec-86c8-1bb2d32f5b79%40jonrowe.co.uk. For more options, visit https://groups.google.com/d/optout.
