2012-06-30 31 views
1

最近我一直在挖掘Ruby,并且正在努力将一些东西扔在一起,以便使用我正在学习的东西。在Sinatra测试随机输出

我有一个输出随机报价的Sinatra应用程序。我想对此进行RSpec测试,因为这似乎是正确的道路。

为了测试类,我做了这样的事情:

it "prints a random line" do 
    QuoteFile.any_instance.stub(:random).and_return(@quote.to_s) 

    @quotefile.random.should == "Sample quote" 
end 

所以我已经采取了这一点,并把它变成一个基本西纳特拉的应用程序。我的RSpec的文件看起来像这样:

describe 'Quote App' do 
    include Rack::Test::Methods 

    def app 
     Sinatra::Application 
    end 

    it "prints random quote" do 

     get '/' 
     ???? 
    end 
end 

我的问题是:我如何存根出在“获取 '/' 随机方面

谢谢你们

+0

怎么样使用像webmock工具 – allenwei

回答

2

使用

QuoteFile.any_instance.stub(:random).and_return("This is a random quote") 

在您的Sinatra测试中,并检查输出结果如下:

describe 'Quote App' do 
    include Rack::Test::Methods 

    def app 
    Sinatra::Application 
    end 

    it "prints random quote" do 
    QuoteFile.any_instance.stub(:random).and_return("This is a random quote") 
    get '/' 
    last_response.body.should =~ /This is a random quote/ 
    end 
end 
+0

哦哇..我想我应该只是试过相同的代码。这工作。谢谢一堆! –