2008-11-11 29 views
5

Hãy nói rằng tôi có đoạn mã sau vào application_helper.rb:tìm nạp 'ACTION_NAME' hoặc 'điều khiển' từ helper đặc tả

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

mà sẽ làm một cái gì đó nếu gọi trong action index.

Hỏi: Làm cách nào để viết lại thông số trợ giúp cho điều này trong application_helper_spec.rb để mô phỏng cuộc gọi từ hành động 'chỉ mục'?

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 

Trả lời

7

Bạn có thể còn sơ khai ACTION_NAME phương pháp để bất cứ điều gì giá trị mà bạn muốn:

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

còn sơ khai! tại helper, damn bỏ lỡ cố gắng mà ra, tôi đã khai! (: action_name) .and_return ('index'), không helper.stub! (: action_name) .and_return ('index') Cảm ơn rsim :) – edthix

Các vấn đề liên quan