How to test a method call in a constructor with rspec

rspec stub class method
rspec allow
rspec double
rspec mock instance method
rspec stub method
rspec class method
rspec mock module method
rspec expect method to be called

i have a constructor like this:

class Foo
  def initialize(options)
    @options = options
    initialize_some_other_stuff
  end
end

and want to test the call to initialize_some_other_stuff if a instantiate a new Foo object.

I found this question rspec: How to stub an instance method called by constructor? but the suggested solution to call Foo.any_instance(:initialize_some_other_stuff) does not work in my rspec version (2.5.0).

Can anyone help me to test this constructor call?

In you spec, you could have the following:

class Foo
  attr_reader :initializer_called
  def initialize_some_other_stuff
    @initializer_called = true
  end
end

foo = Foo.new
foo.initializer_called.should == true

If the constructor calls the initiaize_some_other_stuff method, foo.initializer_called should be true.

How to test a method call in a constructor with rspec, and want to test the call to initialize_some_other_stuff if a instantiate a new Foo object. I found this question rspec: How to stub an instance method called by  Useful RSpec trick for testing method with arguments. Nov 1, 2017. What’s inside: A useful rspec/rspec-its trick for testing methods with arguments + philosophical explanations why I consider such tricks a good thing. The task. Currently we are working hard on daru’s next version, and part of this work is refactoring specs. Most of them are

Here you go:

stub_model(Foo).should_receive(:some_method_call).with(optional_argument)

A pattern for testing class methods in ruby with rspec explicit subjects, of this class (with no arguments passed to the constructor). If you want subject thus provides a decent syntax for testing method calls directly. # Sometimes  And you’ll get your first passing test:. Finished in 0.00315 seconds (files took 0.09083 seconds to load) 1 example, 0 failures This is what we call test-driven development (TDD). You write the tests first, then let the tests guide you on what you need to do next. RSpec Let Method

Since the initialize_some_other_stuff method is private to the class, you should not care if it executes or not. That said, if that method performs some expensive operation that you don't want your test waiting for, then it is quite okay to mock that operation.

So, if Foo looked like this:

class Foo
  attr_reader :options, :other_stuff

  def initialize(options)
    @options = options
    initialize_some_other_stuff
  end

  def initialize_some_other_stuff
    @other_stuff = Bar.new.long_running_operation
  end
end

Then you could mock out the call to Bar#long_running_operation like this:

describe Foo do
  subject(:foo) { described_class.new(options) }

  let(:options) { 'options' }
  let(:bar) { instance_double(Bar, long_running_operation: 42) }

  before do
    allow(Bar).to receive(:new).and_return(bar)

    foo
  end

  it 'initializes options' do
    expect(foo.options).to eq(options)
  end

  it 'initializes other stuff' do
    expect(foo.other_stuff).to eq(bar.long_running_operation)
  end
end

Now, you're testing the assignments. But, you're not waiting on the expensive operation to complete.

How to stub initialize method on object · Issue #1152 · rspec/rspec , Lets we have class class Publish def initialize(options) end def call end end service = Publish.new(a: 1, b: 2) service.call How to test that  How to test a private method in rspec by Ryan Sukale · August 11, 2016 Since you cannot access a private method directly, in order to test a private method, you can make use of the ‘send’ method that is available on all objects.

Using a class double - Verifying doubles - RSpec Mocks, commonly called "class methods", not "module methods", even when working with a module. Background. Given: a file named "lib/user.rb" with: class User  do_some_math (1, 2) def do_some_math (x, y) return add (x, 7) end private def add (x, y) return x+y end. You can test that do_some_math returns 3. Because do_some_math calls add. level 1. myliverhatesme. 2 points · 3 years ago.

RSpec Practices, RSpec, like other test frameworks, provides helper methods for test setup and test describe Client do describe '#initialize' do it 'should respond with default values​' do RSpec has the convention that when you call should without an explicit  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How do I call a module method in rspec, with the current module set up

Java: Constructors should only call non-overridable methods, Calling an overridable method from a constructor could result in failures or strange behaviors when instantiating a subclass which overrides the method. But, given your question, I don't see why you couldn't just use an Rspec double. I'll show you how below. But, first, it would be a bit easier to test the code if it were not all in the constructor. github_api.rb

Comments
  • Hi gnab, thx for your answer. The problem is, that i do not want the method initialize_some_other_stuff to be called, i want to mock it.
  • I understand. The code I posted effectively replaces initialize_some_other_stuff with a dummy (mock), and adds a little functionality for being able to determine if it has been called, needed since we cannot apply any listeners for this as the call takes place in the constructor. Your original initialize_some_other_stuff is never called.