2013-09-25 51 views
1

我正在編寫一個RSpec規範來測試一個Rails助手。問題是我測試的幫助器方法取決於在另一個幫助器中定義的方法。這對我來說可能是一種代碼味道,但是我正在爲遺留代碼添加測試,而不是我可以重構的點。我如何測試這個Rails幫助器?如何測試依賴於另一個Rails助手的Rails助手?

module FancyHelper 
    def fancy_method 
    html = "hello" 
    html << another_fancy_method 
    html 
    end 
end 

module OtherHelper 
    def another_fancy_method 
    "world" 
    end 
end 

require "spec_helper" 
describe FancyHelper do 
    describe "#fancy_method" do 
    it "does what it's supposed to" do 
     helper.fancy_method.should match("hello") 
     # NoMethodError: undefined method `another_fancy_method' 
    end 
    end 
end 

回答

2

這是哪些存根。在測試依賴於其他助手的幫助程序時,您需要將其他幫助程序存根以獲取可預測的值並完成測試。

編輯:https://www.relishapp.com/rspec/rspec-mocks/docs/method-stubs感謝grantovich爲新的鏈接。

+1

該doc鏈接適用於舊版本的RSpec。 [這是當前版本。](https://www.relishapp.com/rspec/rspec-mocks/docs/method-stubs) – Grantovich