开发者

In Sinatra - does anyone use test fixtures? how is your test suite set up?

I'm coming from a开发者_StackOverflow Ruby/Rails world. I'm getting testing set up on a Sinatra project (with Rack::Test). I usually use Fixtures in testing. Is there an equivalent for Sinatra?

How do people set up their Sinatra test suites (outside of the basic helloworld example that is the only example I can find for Sinatra tests).

Thanks!


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


ActiveRecord includes support for fixtures, you just have to wire them up in 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 fixtures on your 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'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

Configure Rake to look for 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've posted a small example application to demonstrate using Sinatra, ActiveRecord, and test fixtures.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜