【问题标题】:Testing random output in Sinatra在 Sinatra 中测试随机输出
【发布时间】:2012-06-30 23:32:15
【问题描述】:

我最近一直在深入研究 Ruby,并且正在努力将一些东西放在一起以使用我所学的东西。

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

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

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

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

所以我把它放到了一个基本的 Sinatra 应用程序中。我的 RSpec 文件如下所示:

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

    def app
        Sinatra::Application
    end

    it "prints random quote" do

      get '/'
         ????
    end
end

我的问题是:如何在“get '/' 中剔除随机方面?

谢谢大家!

【问题讨论】:

  • 如何使用 webmock 之类的工具

标签: ruby rspec sinatra


【解决方案1】:

使用

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

【讨论】:

  • 哦哇..我想我应该尝试相同的代码。那行得通。非常感谢!
猜你喜欢
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多