Coder Social home page Coder Social logo

Comments (7)

aldesantis avatar aldesantis commented on August 16, 2024 1

@Rtwena I tried to reproduce the issue with the following spec:

require 'spec_helper'

RSpec.describe 'Mocking' do
  before(:all) do
    module PrependersTest
      class BaseClass; end
    end

    module PrependersTest::AddClassMethod
      include Prependers::Prepender.new

      module ClassMethods
        def new_method; end
      end
    end
  end

  before do
    allow(PrependersTest::BaseClass).to receive_messages(new_method: 'test')
  end

  it 'works when mocking a class method' do
    expect(PrependersTest::BaseClass.new_method).to eq('test')
  end
end

However, the test passes with no errors being returned either during or after the test.

Do you have a Git repo I can use to reproduce the issue?

from prependers.

aldesantis avatar aldesantis commented on August 16, 2024 1

Interesting... This will require digging deep into the internals of RSpec's verify_partial_doubles to understand why it thinks the method doesn't exist. 😩 I'll try to take a look next week and let y'all know!

from prependers.

Rtwena avatar Rtwena commented on August 16, 2024

The above issues can be mitigated by using
base.extend ClassMethods instead of singleton_class.prepend

However, with base.extend you cannot overwrite existing class methods.
You will have to use singleton_class.prepend

from prependers.

Rtwena avatar Rtwena commented on August 16, 2024

I'm going to close this issue and reopen when I have actual proof/specs.

from prependers.

vassalloandrea avatar vassalloandrea commented on August 16, 2024

Found a similar bug:

app/prependers/controllers/spree/taxons_controller/add_autocomplete.rb

# frozen_string_literal: true

module Spree::TaxonsController::AddAutocomplete
  def autocomplete
    render json: ::Spree::Taxon.autocomplete(query: params[:keywords] || nil)
  end
end

app/prependers/models/spree/taxon/add_searchkick.rb

# frozen_string_literal: true

module Spree::Taxon::AddSearchkick
  def self.prepended(base)
    base.searchkick suggest: [:name], word_start: [:name]
  end

  def search_data
    {
      name: name&.strip,
      permalink: permalink,
      parent_name: parent&.name&.strip
    }
  end

  module ClassMethods
    def autocomplete(query:)
      search(
        query,
        limit: 10,
        load: false,
        misspellings: { below: 3 },
      ).map do |result|
        {
          name: result.name,
          permalink: result.permalink,
          parent_name: result.parent_name,
        }
      end.uniq
    end
  end
end

Error running spec/requests/spree/taxons_controller_spec.rb:

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Spree::TaxonsController, type: :request do
  describe 'GET /autocomplete/taxons' do
    subject(:req) { ->(keywords) { get(spree.taxons_autocomplete_path(keywords: keywords)) } }

    before do
      allow(::Spree::Taxon).to receive(:autocomplete)
    end

    it 'calls the autocomplete method with the passed keywords' do
      req.call('Pump')
      expect(Spree::Taxon).to have_received(:autocomplete).with(query: 'Pump')
    end

    it 'calls the autocomplete method with the passed keywords' do
      req.call(nil)
      expect(Spree::Taxon).to have_received(:autocomplete).with(query: nil)
    end
  end
end

image

from prependers.

nerfologist avatar nerfologist commented on August 16, 2024

Hi, I am experiencing a similar issue. My code prepended to Spree::Carton:

module Cometeer
  module Spree
    module Carton
      module AddNeedsDryIce
        def needs_dry_ice?
          self.class.needs_dry_ice.exists?(id: id)
        end

        module ClassMethods
          def needs_dry_ice
            # some business logic
          end
        end
      end
    end
  end
end

The prepended class method working in the console:

cometeer:development(main):001:0> Spree::Carton.needs_dry_ice
  Spree::Carton Load (20.5ms)  SELECT DISTINCT "spree_cartons".* FROM "spree_cartons" ...
=> #<ActiveRecord::Relation [#<Spree::Carton id: 2, number: "C03108044687" ...

In my test:

allow(::Spree::Carton).to receive(:needs_dry_ice) { ::Spree::Carton.all }

When running the test:

NameError:
    undefined method `needs_dry_ice' for class `#<Class:0x00007fa771d8c690>'

from prependers.

FrancescoAiello01 avatar FrancescoAiello01 commented on August 16, 2024

I ran into the problem with basically the same setup as @nerfologist, just wanted to keep this thread alive ☠️

from prependers.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.