1

I'm trying to put the rspec for active link condition present in the application_helper.rb. Application_helper.rb code:

def active_class(link_path) current_page?(link_path) ? 'active' : '' end 

i tried to put the rspec for this active_class method, i used stub for this purpose.This is my rspec code for active_class method. Application_helper_spec.rb code:

describe 'when called from "index" action' do before helper.stub!(:action_name).and_return('index') end it 'should do' do helper.active_class.should == 'active' end describe 'when called from "other" action' do before helper.stub!(:action_name).and_return('other') end it 'should do' do helper.active_class.should == 'empty' end 

I'm getting the error as undefined method stub.How can i get overcome from this issue?

1 Answer 1

2

I use slightly different interface for stubbing and mocking which looks like this:

allow(helper).to receive(:action_name).and_return('index') 

When you just want to stub the response.

But if you need to set up an expectation:

expect(helper).to receive(:action_name).and_return('index') 

You can find more info in the docs: https://relishapp.com/rspec/rspec-mocks/v/3-7/docs/basics

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response,when i add this code in my condition, i got the error as undefined method 'to'. Is there any better way to change this?
That's weird because it's part of standard rspec. Can you try the same with valilla rails + rspec app? Maybe some of your gems, or other config is interfering with rspec mock/stub lib?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.