In Sinatra - does anyone use test equipment? How is your test suite configured?

I come from the world of Ruby / Rails. I get testing configured on a Sinatra project (with Rack :: Test). Usually I use Fixtures in testing. Is there an equivalent to Sinatra?

How people customize their Sinatra test suites (outside of the main helloworld example, which is the only example I can find for Sinatra tests).

Thanks!

+7
testing rack sinatra
source share
2 answers

I use Machinist for this (and Rails, too. Hate YAML fixtures.)

+4
source share

ActiveRecord includes support for fixtures, you just need to connect them to 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 

Then you can use the instruments in the test classes.

 # test/unit/blog_test.rb require_relative '../test_helper' class BlogTest < ActiveSupport::TestCase fixtures :blogs def test_create blog = Blog.create(:name => "Rob Writing") assert_equal "Rob Writing", blog.name end def test_find blog = Blog.find_by_name("Jimmy Jottings") assert_equal "Stuff Jimmy says", blog.tagline end end 

Configure Rake to search your tests in the right places.

 # 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 

I placed the application a small application to demonstrate the use of Sinatra, ActiveRecord and test devices.

+1
source share

All Articles