Rebel
Rebel

Reputation: 797

Stub method called by other method

run calls ping, which normally would set network_status = true. Then it should call connect if that is false:

def run
  ping
  if @network_status == false
    connect
  end
end

I want to test for it, and I wrote this:

t = Test.new

#other tests happen inside the run method, then

it "calls .connect" do
  t.stub(:network_status).and_return(false)
  t.stub(:ping).and_return(false)
  expect(t).to receive(:connect)
  t.run
end

but the result is:

 Failure/Error: expect(Test).to receive(:connect)
   (<Test (class)>).connect(any args)
       expected: 1 time with any arguments
       received: 0 times with any arguments

Why?

Upvotes: 0

Views: 40

Answers (1)

usha
usha

Reputation: 29369

@network_status is not a method. It is an instant variable. You should set the instant variable in the test.

it "calls .connect" do
  t.instance_variable_set(:@network_status, false)
  t.stub(:ping).and_return(false)
  expect(t).to receive(:connect)
  t.run
end

Upvotes: 2

Related Questions