جلب "ACTION_NAME" أو "تحكم" من المواصفات المساعد
-
07-07-2019 - |
سؤال
ودعونا نقول أن لدي البرمجية التالية في <قوية> application_helper.rb : ل
def do_something
if action_name == 'index'
'do'
else
'dont'
end
end
والتي سوف تفعل شيئا إذا دعا في عمل المؤشر.
س: كيف يمكنني كتابة المواصفات المساعد لهذا في على application_helper_spec.rb لمحاكاة مكالمة من العمل "مؤشر '
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
المحلول
ويمكنك كعب ACTION_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
لا تنتمي إلى StackOverflow