mardi 6 juin 2023

Overriding module's private method which included to controller from external gem in Rails's Rspec test

I want to override private method as same as i wrote at title.

I have rspec controller test code, and i have to change some part of code.

I'm testing controller's method, and it is using external gem's private method.

  • Structure

External gem
┗ Module A
┗ Module B (inside Module A)
┗ private method Z
# def Z
      puts 1
      puts 2
      puts 3
end

Rails App
┗ Controller 1
┗ public action method login
┗ #login method calls A::B::Z


Rspec test
┗ Controller 1 (RSpec.describe Controller_1, type: :controller ...)
┗ test login (describe '#login' ..., context 'with wrong method..')

# I WANT TO CHANGE 'method Z' IN HERE LIKE
def Z
      puts 1
      puts 100
      puts 3
end


Can i do this in Rspec in Rails application?




if i use stub like this,

  before do
    allow(controller).to receive(:method_Z) #and contains puts 1, puts 100, puts3 ...
  end

  expect(response) #contains puts 1, puts 100, puts3 ...

i thought test would not be as i expected, so i'm wondering how to override method in rspec..

Aucun commentaire:

Enregistrer un commentaire