10

레일 2에서 레일 3 어플리케이션으로 업그레이드 중입니다. 잘 테스트 된 코드는 shoulda와 Test :: Unit을 사용하고 매크로 should_create와 should_change를 광범위하게 사용합니다.Rails 3 사용 중단 경고를 선택적으로 음소거 할 수 있습니까?

저는 shoulda 관리자가 두 가지 방법을 모두 없애고 싶지만 Test :: Unit을 사용하는 사람들은 필요하다고 생각하지 않는다는 것을 알고 있습니다. (전체 토론을 이해하고 있는지는 확실하지 않습니다.) this discussion.

Anaway, 특정 매크로에 대한 비추천 경고를 선택적으로 전환 할 수있는 방법이 있습니까? 당신의 테스트 환경 파일에

ActiveSupport::Deprecation.silenced = true 

나는 또한 블록 코드의 특정 부분을 넣을 수 있다는 사실을 알고 : 난 이미 당신이 완전히 설정하여 레이크 테스트 출력에서 ​​중단 경고를 해제 할 수 this posting 알고 그들에게 침묵으로 활용하려면 다음

ActiveSupport::Deprecation.silence do 
# no warnings for any use of deprecated methods here 
end 

후자는 옵션입니다하지만 모든 테스트를 통해 이동과 같은 블록에 should_create 매크로를 둘러싸 저를 필요로한다. 그래서 한 가지 구성 설정으로 특정 매크로에 대한 경고를 완전히 없애는 방법이 있는지 궁금합니다.

답변

3

은 아마 훨씬 더 우아하고있다. 대부분의 것을 피하기 위해, 나는 test_helper.rb에서 Deprecation :: warn 메소드를 덮어 썼다. 그래서 그 대신 이전 코드, 사용 :

module ActiveSupport 
    module Deprecation 
    class << self 
     def warn(message = nil, callstack = caller) 
     # modif pvh the following lines make sure no deprecation warnings are sent 
     # for code that is 
     # not by my but in some gem or plugin... 
     return if silenced || callstack.grep(/myrailsappname/).blank? 
     # return if silenced 
     deprecation_message(callstack, message).tap do |m| 
      behavior.each { |b| b.call(m, callstack) } 
     end 
     end 
    end 
    end 
end 

가 BTW 앱의 이름 (이에있는 폴더의 이름)와 myrailsappname를 교체해야합니다. 그 이름을 얻는 방법이 좀 더 일반적인 방법 일지 모르지만 지금은 찾을 수 없습니다.

0

해결책을 찾았습니다. test/test_helper.rb에서 모듈을 다시 열고 매크로 정의를 동일한 정의로 덮어 쓰지 만 비추천 경고는 주석 처리했습니다. 내가 STIL 플러그인이나 내가 설치 한 보석에 있던 코드에서 다른 사용 중단 경고를 많이했다 사실이 생각을 할 수있는 방법 ...

# modif pvh DEPREC 
# reopen the module and silence the deprecation statement to avoid 
# having my results overflown by these deprecation warnings... 
module Shoulda # :nodoc: 
    module Macros 
    def should_create(class_name) 
     ##::ActiveSupport::Deprecation.warn 
     should_change_record_count_of(class_name, 1, 'create') 
    end 
    end 
end 
6

오래된 질문 -하지만 당신은 새로운 당신이 선택적으로 무시하고 싶은 감가 상각이있는 경우 :

ActiveSupport::Deprecation.behavior = lambda do |msg, stack| 
    unless /LIBRARY_NAME/ =~ msg 
    ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:stderr].call(msg,stack) # whichever handlers you want - this is the default 
    end 
end 

ActiveSupport 3을 위해입니다.

+2

여전히 ActiveSupport 4에서 작동합니다. –

2

대안을 권장 할 수 있습니까?

module ActiveSupport 
    class Deprecation 
    module Reporting 
     # Mute specific deprecation messages 
     def warn(message = nil, callstack = nil) 
     return if message.match(/Automatic updating of counter caches/) 

     super 
     end 
    end 
    end 
end 
관련 문제