-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue Mocking Prepended Class Methods #6
Comments
The above issues can be mitigated by using However, with |
@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? |
I'm going to close this issue and reopen when I have actual proof/specs. |
Found a similar bug:
# frozen_string_literal: true
module Spree::TaxonsController::AddAutocomplete
def autocomplete
render json: ::Spree::Taxon.autocomplete(query: params[:keywords] || nil)
end
end
# 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 # 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 |
Hi, I am experiencing a similar issue. My code prepended to 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:
In my test: allow(::Spree::Carton).to receive(:needs_dry_ice) { ::Spree::Carton.all } When running the test:
|
Interesting... This will require digging deep into the internals of RSpec's |
I ran into the problem with basically the same setup as @nerfologist, just wanted to keep this thread alive ☠️ |
Ruby: 2.5.3
Prependers: 0.1.1
Rails: 5.2.2
Given the follow file
When trying to mock
Spree::Product.purchasable
The follow error is returned AFTER the test has ran
When trying to mock via a class_double
The follow error is returned During other tests that are using
Spree::Product
When trying to force reset the mock using
The follow error is returned AFTER the test has ran
Maybe related to rspec/rspec-mocks#1218
The text was updated successfully, but these errors were encountered: