代码之家  ›  专栏  ›  技术社区  ›  edthix

正在从帮助程序规范获取“action_name”或“controller”

  •  5
  • edthix  · 技术社区  · 16 年前

    假设我输入了以下代码 应用程序帮助程序.rb :

    def do_something
     if action_name == 'index'
       'do'
     else
       'dont'
     end
    end
    

    如果在index操作中调用,它将做一些事情。

    问:如何在 应用程序帮助程序规格RB 模拟“index”操作的调用?

    describe 'when called from "index" action' do
      it 'should do' do
        helper.do_something.should == 'do' # will always return 'dont'
      end
    end
    
    describe 'when called from "other" action' do
      it 'should do' do
        helper.do_something.should == 'dont'
      end
    end
    
    1 回复  |  直到 9 年前
        1
  •  7
  •   Raimonds Simanovskis    16 年前

    您可以将action\u name方法存根为所需的任何值:

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