سؤال

ودعونا نقول أن لدي البرمجية التالية في <قوية> 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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top