2008-11-11 12 views
5

Załóżmy, że mam następujący kod w application_helper.rb:pobierania „ACTION_NAME” lub „administrator danych” od pomocnika specyfikacji

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

który zrobi coś, jeśli nazywa się wewnątrz akcji index.

Q: Jak mogę przepisać spec pomocnika za to w application_helper_spec.rb symulować połączenia z „index” działania?

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 

Odpowiedz

7

Można skrótową ACTION_NAME do sposobu niezależnie od wartości chcesz:

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 
+0

niedopałek! na pomocnika, cholernie nieodebrane to wypróbowałem, zrobiłem stub! (: action_name) .and_return ('index'), nie helper.stub! (: action_name) .and_return ('index') Dzięki rsim :) – edthix