Nie można stub metody pomocniczej z rspec
Usiłuję odgadnąć metodę na pomocniku zdefiniowanym w moim kontrolerze. Na przykład:
<code>class ApplicationController < ActionController::Base def current_user @current_user ||= authenticated_user_method end helper_method :current_user end module SomeHelper def do_something current_user.call_a_method end end </code>
W moim Rspec:
<code>describe SomeHelper it "why cant i stub a helper method?!" do helper.stub!(:current_user).and_return(@user) helper.respond_to?(:current_user).should be_true # Fails helper.do_something # Fails 'no method current_user' end end </code>
Wspec/support/authentication.rb
<code>module RspecAuthentication def sign_in(user) controller.stub!(:current_user).and_return(user) controller.stub!(:authenticate!).and_return(true) helper.stub(:current_user).and_return(user) if respond_to?(:helper) end end RSpec.configure do |config| config.include RspecAuthentication, :type => :controller config.include RspecAuthentication, :type => :view config.include RspecAuthentication, :type => :helper end </code>
Zadałem podobne pytanietutaj, ale zdecydowałem się na pracę. To dziwne zachowanie pojawiło się ponownie i chciałbym to zrozumiećczemu to nie działa.
AKTUALIZACJA: Znalazłem to powołaniecontroller.stub!(:current_user).and_return(@user)
przedhelper.stub!(...)
to właśnie powoduje to zachowanie. Łatwo to naprawićspec/support/authentication.rb
, ale czy to błąd w Rspec? Nie widzę powodu, dla którego można by oczekiwać, że nie będzie w stanie odgadnąć metody na pomocniku, jeśli został on już zablokowany na kontrolerze.