如何使用Minitest存储Sinatra应用程序中使用的方法?

时间:2014-06-21 12:45:13

标签: sinatra stub minitest

我想测试一个小的Sinatra应用程序。要做到这一点,就要找出一些行为。

Sinatra应用程序中的相关代码是:

require 'sinatra'
require 'yaml'

get '/commands' do
  YAML.load_file("commands.yml")["commands"].join("\n")
end

要测试的minitest/spec代码是:

include Rack::Test::Methods
def app
  Sinatra::Application
end

describe "/commands" do
  describe "with two commands defined" do
    it "must list the two commands" do
      YAML.stub(:load_file, {"commands" => ["hostname", "uptime"]}) do
        get '/commands'
        last_response.must_be :ok?
        last_response.body.must_equal "hostname\nuptime"
      end
    end
  end
  describe "with no commands defined" do
    YAML.stub(:load_file, {"commands" => nil}) do
      it "must list no commands" do
        get '/commands'
        last_response.body.must_equal ""
      end
    end
  end
end

使用小型spec_helper:

ENV['RACK_ENV'] = 'test'
require 'minitest/autorun'
require 'minitest/spec'
require 'rack/test'

require_relative '../minion' #This is the sinatra-app.

YAML.stub部分是最重要的部分,因为它不是work as expected。它确实在规范中存在YAML.load_file。但是在sinatra-app的上下文中,YAML.load_file未被取消并加载commands.yml,尽管我试图将其删除。

如何使用YAML.load_fileminitest/spec的调用进行存根,并在Sinatra应用程序中使用该存根?

0 个答案:

没有答案