我來自Ruby/Rails世界。我正在進行Sinatra項目的測試(使用Rack :: Test)。我通常在測試中使用Fixtures。 Sinatra有沒有相當於?在Sinatra - 有沒有人使用測試夾具?你的測試套件如何設置?
人們如何設置他們的Sinatra測試套件(基本的helloworld示例以外,這是我可以找到的用於Sinatra測試的唯一示例)。
謝謝!
我來自Ruby/Rails世界。我正在進行Sinatra項目的測試(使用Rack :: Test)。我通常在測試中使用Fixtures。 Sinatra有沒有相當於?在Sinatra - 有沒有人使用測試夾具?你的測試套件如何設置?
人們如何設置他們的Sinatra測試套件(基本的helloworld示例以外,這是我可以找到的用於Sinatra測試的唯一示例)。
謝謝!
我用Machinist這個(和Rails,也。我恨YAML的固定裝置。)
的ActiveRecord包括燈具的支持,你就必須將它們連接起來test_helper.rb
。
# test/test_helper.rb
require_relative '../app'
require 'minitest/autorun'
require 'active_record'
ActiveRecord::Base.establish_connection(:test)
class ActiveSupport::TestCase
include ActiveRecord::TestFixtures
include ActiveRecord::TestFixtures::ClassMethods
class << self
def fixtures(*fixture_set_names)
self.fixture_path = 'test/fixtures'
super *fixture_set_names
end
end
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false
end
然後你可以在你的測試類上使用燈具。
# test/unit/blog_test.rb
require_relative '../test_helper'
class BlogTest < ActiveSupport::TestCase
fixtures :blogs
def test_create
blog = Blog.create(:name => "Rob's Writing")
assert_equal "Rob's Writing", blog.name
end
def test_find
blog = Blog.find_by_name("Jimmy's Jottings")
assert_equal "Stuff Jimmy says", blog.tagline
end
end
配置Rake以在正確的位置查找您的測試。
# Rakefile
require_relative './app'
require 'rake'
require 'rake/testtask'
require 'sinatra/activerecord/rake'
Rake::TestTask.new do |t|
t.pattern = "test/**/*_test.rb"
end
task default: :test
我已經發布了small example application使用西納特拉,ActiveRecord的,和測試夾具來證明。