Testing the mixture in ruby

I am trying to write an rspec test for mixin class. I have the following.

 module one
  module two

    def method
      method_details = super
      if method_details.a && method_details.b
         something
      elsif method_details.b
        another thing
      else
        last thing
      end
    end

  end
end

Now I mocked the "method" object, which will be passed to the class. But I'm trying to access the super method. I did,

let(:dummy_class) { Class.new { include one::two } }

How to pass a cheated methodobject to this dummy class?

How can I check this? New to rubies, can someone show me the direction with this.

Thanks in advance.

UPDATE:

I tried,

let(:dummy_class) {
    Class.new { |d|
      include one::two
      d.method = method_details
    }
  }

let (:method_details){
'different attributes'
}

still not working. I getundefined local variable or method method_details for #<Class:0x007fc9a49cee18>

+4
source share
2 answers

I personally test mixing with the class. Since the mixer itself (module) does not matter if it is not tied to a class / object.

Example:

module SayName
  def say_name
     p 'say name'
  end
end


class User
  include SayName
end

Therefore, I believe that you should test your module with reference to the corresponding class / object.

0

, , super #method " super " .

:

:

module One
  module Two
    def the_method
      method_details = super
      if method_details.a && method_details.b
        'something'
      elsif method_details.b
        'another thing'
      else
        'last thing'
      end
    end
  end
end

RSpec.describe One::Two do
  require 'ostruct'

  let(:one_twoable) { Class.new(super_class) { include One::Two }.new }

  describe '#the_method' do
    let(:the_method) { one_twoable.the_method }

    context 'when method_details#a && method_details#b' do
      let(:super_class) do
        Class.new do
          def the_method
            OpenStruct.new(a: true, b: true)
          end
        end
      end

      it 'is "something"' do
        expect(the_method).to eq('something')
      end
    end

    context 'when just method#b' do
      let(:super_class) do
        Class.new do
          def the_method
            OpenStruct.new(a: false, b: true)
          end
        end
      end

      it 'is "another thing"' do
        expect(the_method).to eq('another thing')
      end
    end

    context 'when neither of the above' do
      let(:super_class) do
        Class.new do
          def the_method
            OpenStruct.new(a: false, b: false)
          end
        end
      end

      it 'is "last thing"' do
        expect(the_method).to eq('last thing')
      end
    end
  end
end
0

All Articles