Activerecord NoMethodError:未定义的方法`stub';零级:零级

Activerecord NoMethodError:未定义的方法`stub';零级:零级,activerecord,rspec,specifications,Activerecord,Rspec,Specifications,我正试图在application_helper.rb中放置活动链接条件的rspec。 应用程序\u helper.rb代码: def active_class(link_path) current_page?(link_path) ? 'active' : '' end 我试图为这个active_类方法放置rspec,为此我使用了存根。这是我的active_类方法的rspec代码。 应用程序\u帮助程序\u spec.rb代码: describe 'when called from

我正试图在application_helper.rb中放置活动链接条件的rspec。 应用程序\u helper.rb代码:

def active_class(link_path)
    current_page?(link_path) ? 'active' : ''
  end
我试图为这个active_类方法放置rspec,为此我使用了存根。这是我的active_类方法的rspec代码。 应用程序\u帮助程序\u spec.rb代码:

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

我将错误作为未定义的方法存根。如何克服此问题?

我使用稍微不同的界面进行存根和模拟,如下所示:

allow(helper).to receive(:action_name).and_return('index')
当您只想存根响应时

但如果您需要设定期望值:

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

您可以在文档中找到更多信息:

感谢您的回复,当我在我的条件中添加此代码时,我将错误作为未定义的方法“to”。有没有更好的方法来改变这一点?这很奇怪,因为它是标准rspec的一部分。你能用valilla rails+rspec应用程序尝试同样的方法吗?也许您的一些gem或其他配置正在干扰rspec mock/stub lib?