Use refinements inside RSpec

Say I have a clarification

module RefinedString
  refine String do
    def remove_latin_letters
      #code code code code
    end
  end
end

and I use it inside my class. Speech:

class Speech
  using RefinedString
  def initialize(text)
    @content = text.remove_latin_letters
  end
end

I wrote tests for refinement in RSpec, and now I'm testing Speech class

describe Speech
  let(:text) { "ąńńóyińg" }

  it 'should call my refinement' do
    expect(text).to receive(:remove_latin_letters)
    Speech.new(text)
  end
end

but i get RSpec::Mocks::MockExpectationError: "ąńńóyińg" does not implement: remove_latin_letter

I don’t think this is taunting, this is a good solution (but I can be wrong! Is the solution taunting here?)

so i tried

let(:text) { described_class::String.new("ąńńóyińg") } but the result is the same.

I don’t want to explicitly call using RefinedStringinside my RSpec (it should understand this on its own, right?)

How to make RSpec aware of my exquisite techniques?

+4
source share
1 answer

, . , , , . , , -. , , ( ).

, :

class TestClass
  attr_reader :content
  def initialize(text)
    @content = text.remove_latin_letters
  end
end

describe "when not using RefinedString" do
  it "raises an exception" do
    expect { TestClass.new("ąńńóyińg") }.to raise_error(NoMethodError)
  end
end

class RefinedTestClass
  using RefinedString
  attr_reader :content
   def initialize(text)
     @content = text.remove_latin_letters
  end
end

describe "when using RefinedString" do
  it "removes latin letters" do
    expect(RefinedTestClass.new("ąńńóyińg").content).to eq "ńńóń"
  end
end
+7

All Articles