That is because you are calling *invoke_additional_tasks* method on User 
instance. The test case in my example is for *test_method* which is a Class 
method. Please add Class method called *test_method* in User model to 
reproduce the issue.

*user.rb*
class User
attr_accessor :name

class << self
def test_method
rv = validate_something("test")
return rv if rv[:error]

user = User.new
user.name = "test#{rand(1..1000000)}"
user.save!
user.invoke_additional_tasks('test_method')

return { success: true, user: user }
end
end

def invoke_additional_tasks(test)
{ success: true }
end

private

class << self
def validate_something(params)
{ success: true }
end
end
end

*user_spec.rb*
require 'rails_helper'

RSpec.describe User, type: :model do
describe 'test_method' do
context 'when validate_something returns success' do
before(:each) do
allow(User).to receive(:validate_something).and_return(success: true)
end

it 'should invoke_additional_tasks on user' do
expect_any_instance_of(User).to 
receive(:invoke_additional_tasks).with('test_method')
User.test_method
end
end
end
end


On Monday, February 8, 2021 at 9:07:27 PM UTC+5:30 pirj...@gmail.com wrote:

> I can't reproduce the failure with a slightly simplified version.
>
> class User
> attr_accessor :name
>
> def invoke_additional_tasks(test)
> { success: true }
> end
>
> private
>
> class << self
> def validate_something(params)
> { success: true }
> end
> end
> end
>
> RSpec.describe User do
> describe 'test_method' do
> context 'when validate_something returns success' do
> before(:each) do
> allow(User).to receive(:validate_something).and_return(success: true)
> end
>
> it 'should invoke_additional_tasks on user' do
> expect_any_instance_of(User).to
> receive(:invoke_additional_tasks).with('test_method')
>
> user = User.new
> user.name = "test#{rand(1..1000000)}"
> user.invoke_additional_tasks('test_method')
> end
> end
> end
> end
>

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rspec+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/1dfe0ef4-6f06-4d77-a3aa-a9ed0cba2bb2n%40googlegroups.com.

Reply via email to